我正在尝试在 Unity 中制作 2D 游戏,并且在 FixedUpdate() 函数中使用 Input.GetMouseButtonDown() 方法。我希望我的播放器改变水平方向,因此我有以下代码。
if (Input.GetMouseButtonDown(0))
{
if(change == true)
{
rb.velocity = new Vector2(-12,rb.velocity.y);
change=!change;
}
else if(change == false)
{
change=!change;
rb.velocity = new Vector2(12,rb.velocity.y);
}
}
一开始,前 3-6 次点击,效果很好(一次点击改变方向,另一次点击另一个方向)但之后我必须按 2 或 3 次来改变实际方向。
我应该怎么做才能提高改变方向的准确性和质量?
非常感谢您的耐心和关注!
Unity 文档指出您必须在 Update 函数中使用 GetMouseButtonDown()。
您可能应该创建一个全局布尔值来保存值并在 FixedUpdate() 中重置它。像这样的东西:
boolean MouseButtonDown=false;
void Update(){
if(Input.GetMouseButtonDown(0)){
MouseButtonDown=true;
}
}
void FixedUpdate(){
if (MouseButtonDown)
{
if(change == true)
{
rb.velocity = new Vector2(-12,rb.velocity.y);
change=!change;
}
else if(change == false)
{
change=!change;
rb.velocity = new Vector2(12,rb.velocity.y);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句