【发布时间】:2017-09-22 22:27:36
【问题描述】:
我有一些代码在执行时会向前推动一个字符。问题是角色永远不会停止移动并永远持续下去。有没有办法让角色在 2 秒后停止移动?这是我正在使用的代码:
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<Rigidbody>()。在Start()期间调用一次并保存刚体,当你得到一个新的对撞机时对OnTriggerStay执行相同的操作。
标签: unity3d