[英]Unity3D Input Manager And Using Buttons For Input.GetAxis
我刚买了NGUI,需要一些帮助...
现在,我的2D角色正在用左右箭头左右移动。 我在输入管理器中设置了向左和向右箭头。
我使用NGUI制作了向左和向右箭头按钮,将它们添加到我的UI根中。 当我按下向左箭头按钮时,如何模仿被按下的向左箭头? 还是有办法将按下的左箭头按钮添加到输入管理器?
我要做的就是使左箭头按钮的行为类似于我按左箭头键时的行为,而对于右箭头键则相同。
如果没有NGUI,还有另一种方法也可以。 我只希望能够在按移动设备上的左右按钮时左右移动。 我已经在这个问题上停留了大约6个小时,无法弄清楚。
任何帮助将不胜感激! 谢谢!
这是我如何移动角色的示例
float h = Input.GetAxis("Horizontal");
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(h));
// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
if(h * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
if (Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
按钮的效果在OnPress (bool isDown)
函数中处理。 默认情况下,每次将鼠标悬停在按钮上时都会调用它。
如果我对您的理解正确,则想在按键盘上的相关箭头键时模拟这种悬停。
为此,您必须自己调用OnPress (bool isDown)
函数。 尝试将此行添加到您的代码中:
SendMessage("OnPress", true);
这将在添加到游戏对象的所有脚本上调用OnPress函数。
要在特殊的GameObject上调用该函数,只需添加一个公共变量go
,然后再使用go.SendMessage("OnPress", true);
希望这会有所帮助。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.