How to get current value of wheelDelta?

I can only get value of wheelDelta when mousewheel event is trigged.
How can I get the value on demand (If mousewheel event is not trigged), because if I try to save the value in mousewheel event it does not work very well, either it stays 1 or -1. I have no idea how to get its zero state value.

Hi @PC_Coder and welcome,

To get the mouse wheel delta, much like the mouse pointer coordinates, the only way is by subscribing to the mouse events.

This isn’t a restriction of Playcanvas but how browsers provide this information in Javascript. You can’t get the current state of the mouse wheel, since the browser will report only mouse wheel state changes, there is no current state.

As you say you can save the value in a variable and access it at any point from other parts of your script like this:

this.app.mouse.on(pc.EVENT_MOUSEMOVE, function(){
   this.lastMouseWheel = event.wheel;
}, this);

What value are you expecting from the mouse wheel?

It would be +1 or -1 and it works correct but only when the mouse wheel event is fired. My code involves constantly querying for mouse wheel as I need to know its state at any time. So for example on some Frame X the event is fired and I get the +1 value which is correct, but on some other Frame X+t its not fired and it should be 0. I’m unable to get this 0 because at that time event is not fired.

Thanks, it has partially solved my problem. But there is still a slight issue, how can I get updated value when mouse is not moved? How can I reset the value this.lastMouseWheel to 0 in the next frame?

Hi, and welcome to Playcanvas!

You need to separate the logic of updating the mouse wheel value and using that value. When the mouse sends an event, you would save it in a local variable. Your action logic would then use that variable to set the position or what have you. This way, when the mouse scrolls, it will update the value. Your entity would use that value or whatever the last one was, if no event was fired.

For example, a use case: “Move ball on mouse wheel event”. In this case, you would want to separate “move ball” and “mouse move event”. This is unlike the tutorial, where both actions are merged into one for simplicity and brevity of the example.

initialize: function() {
    this.app.mouse.on(pc.EVENT_MOUSEWHEEL, function(){
       this.lastMouseWheel = event.wheel;
    }, this);
},

useMouseWheel: function() {
    // use the new or last one available
    var last = this.lastMouseWheel;
},

udate: function() {
    this.useMouseWheel();
}

Thank you for your response, I need to use current value of wheel scroll (not last value, it won’t be updated if mouse is not scrolled - when it came back to zero)

Something like this? https://playcanvas.com/editor/scene/956659

Thanks it can work

I’m resurrecting this thread because I have this issue occasionally in accessing projects from links in this forum. When I click this project:
https://playcanvas.com/editor/scene/956659

… it opens up in an editor window and I can run it, but it won’t let me look at the script?

You mean this script?

var Test = pc.createScript('test');

// initialize code called once per entity
Test.prototype.initialize = function() {
    var mouse= this.app.mouse;
    
    this._mouseWheelChange = 0;
    
    mouse.on(pc.EVENT_MOUSEWHEEL, function (e) {
        this._mouseWheelChange = e.wheelDelta;
    }, this);
};

// update code called every frame
Test.prototype.update = function(dt) {
    this.entity.translate(0, this._mouseWheelChange * dt * 5, 0);
    this._mouseWheelChange = 0;
};

Yeah, thanks, @Albertos! I see now that when I open a project this way, I can doubleclick the script down in the assets box and open it that way. But I can’t click on the entity and access it through the script component like I can on my own projects.

1 Like

Hi @Michael_McCrickard, this does seem to work on my end? On public projects, when you click the pen icon on the scripts inside the script component of an entity, the button changes to view, and you get a read-only version of the file.

Aha! That’s it. I could tell it was read-only and I could have sworn there was no way to open it. But like you said, it works fine. Thanks for the tip and the video to make it clear! @DevilZ

1 Like