如何获取MouseScroll输入?
我正在尝试在 Unity3D 中进行简单的武器更改。 最好的方法是使用鼠标滚轮,就我所见而言。
我用谷歌搜索了如何执行此操作,发现我必须使用 Input.GetAxis("Mouse ScrollWheel");我使用这段代码并打印它。但我总是得到 0 值。 有什么想法吗?我只需要先解决如何获得价值,剩下的我自己解决。
代码思路:
function Update () {
print(WeaponNumber);
if(Input.GetAxis("Mouse ScrollWheel")){
WeaponNumber += Input.GetAxis("Mouse ScrollWheel");
}
}
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 have 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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嘿朋友,
您可以使用Input.GetAxisRaw 代替Input.GetAxis。 GetAxis 的值经过平滑处理,范围为 -1 .. 1 ,但 GetAxisRaw 为 -1 或 0 或 1。并且您可以删除 If 语句。因为当没有滚轮发生时,该值自动为零。
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.
因此,如果不滚动,Input.GetAxis("Mouse ScrollWheel") 返回 0;如果向上滚动,则返回 0.1;如果向下滚动,则返回 -0.1。因此,此代码的解决方案是:
如果向上滚动,则基本上会增加 WeaponNumber;如果向下滚动,则基本上会减少 WeaponNumber
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:
And that will basically increase WeaponNumber if you scrolled up and decrease if you scrolled down