开发者

How to get MouseScroll Input?

I am trying to make a simple weapon change in Unity3D. The best way to do this is with the mouse Scroll wheel as far as i can see.

I googled on how to do this and found that i hav开发者_StackOverflow社区e to use the Input.GetAxis("Mouse ScrollWheel"); I use this piece of code and print it. Still i always get an value of 0. any ideas? I just need to solve how to get the value first, ill figure out the rest myself.

Code idea:

function Update () {
    print(WeaponNumber);

    if(Input.GetAxis("Mouse ScrollWheel")){
        WeaponNumber += Input.GetAxis("Mouse ScrollWheel");
    }
}


Hey Friend, Instead of Input.GetAxis you may use Input.GetAxisRaw. The value for GetAxis is smoothed and is in range -1 .. 1 , however GetAxisRaw is -1 or 0 or 1. and you may remove the If statement. cause when no scrollwheel happens the value is automatically zero.


So, Input.GetAxis("Mouse ScrollWheel") returns 0 if you didn't scroll, 0.1 if you scroll up and -0.1 if you scrolled down. So solution to this code would be:

    function Update() {
        if (Input.GetAxis("Mouse ScrollWheel") != 0) {
            WeaponNumber += Mathf.FloorToInt(Input.GetAxis("Mouse ScrollWheel") * 10));
        }
    }

And that will basically increase WeaponNumber if you scrolled up and decrease if you scrolled down

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜