【发布时间】:2019-02-13 13:37:13
【问题描述】:
我想做一个简单的脚本,当你点击屏幕时,游戏中的球会向右移动 (20000 * Time.deltaTime),然后如果我再次点击,它会向左移动然后对,以此类推。
我设法让球向右移动,但我需要它在动画完成后等待,因为我需要检查玩家是否再次点击(如果他再次点击,我需要检查移动的方向球)。 我尝试了很多我在网上找到的方法,比如检查 Rigidbody.velocity.magnitude == 0.0f 是否意味着球没有移动..
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);
}
}
}
我希望输出等到动画完成,而是在我单击鼠标的那一刻打印 vaule(x)。
【问题讨论】: