我有一些代码,当它执行时,它会向前推送一个字符。问题在于角色永远不会停止移动并永远持续下去。有没有办法阻止角色在2秒后移动?这里是我正在使用的代码:在Unity中停止更新功能
public class meleeAttack : MonoBehaviour
{
public int speed = 500;
Collider storedOther;
bool isHit = false;
void Start()
{
}
void Update()
{
if (isHit == true)
{
storedOther.GetComponent<Rigidbody>().AddForce(transform.forward * speed);
}
}
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Player" && Input.GetKeyUp(KeyCode.F))
{
storedOther = other;
isHit = true;
}
}
}
我不确定是否有办法停止update()函数,以便停止字符移动。
不要在更新函数中调用'storedOther.GetComponent ()'。在'Start()'过程中调用它一次并保存刚体,当你得到一个新的碰撞体时,对'OnTriggerStay'做同样的事情。 –