我想编写一个简单的脚本,当您点击屏幕时,游戏中的球将向右移动(20000 * Time.deltaTime),然后如果再次点击,它将向左侧移动,然后向右侧移动,依此类推。
我已经成功让球向右移动,但我需要等待动画完成,因为我需要检查玩家是否再次点击(如果是,我需要检查要移动球的方向)。我尝试了许多在网上找到的方法,例如检查 Rigidbody.velocity.magnitude == 0.0f 是否等于0,这意味着球没有移动...
最初的回答:我期望输出等待动画完成后再执行,但实际上,当我点击鼠标时,它立即打印出值(x)。
我希望输出在动画结束后才出现,但实际上,当我点击鼠标时,它就已经输出值(x)了。
我已经成功让球向右移动,但我需要等待动画完成,因为我需要检查玩家是否再次点击(如果是,我需要检查要移动球的方向)。我尝试了许多在网上找到的方法,例如检查 Rigidbody.velocity.magnitude == 0.0f 是否等于0,这意味着球没有移动...
public Rigidbody rb;
public Transform PlayerPosition;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
rb.AddForce(20000 * Time.deltaTime, 0, 0); // moving ball to the right
while (rb.velocity.magnitude != 0.0f) // I tried to check until the ball is not moving
{
}
Debug.Log(PlayerPosition.position.x);
}
}
这是我最新的尝试:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
rb.AddForce(20000 * Time.deltaTime, 0, 0); // moving ball to the right
if(rb.velocity.magnitude < 0.05f) // if i click the ball it just prints it and not wating for the ball to not move
{
Debug.Log(PlayerPosition.position.x);
}
}
}
最初的回答:我期望输出等待动画完成后再执行,但实际上,当我点击鼠标时,它立即打印出值(x)。
我希望输出在动画结束后才出现,但实际上,当我点击鼠标时,它就已经输出值(x)了。
> 0
而不是!= 0
,因为float
几乎永远不会为 0。它们最终会变成类似于 0.000001 的值。 - Eliasar0.05
:rb.velocity.magnitude < 0.05f
。 - Milad Qasemirb.velocity.magnitude > 0.05f
。 - Eliasar