【发布时间】:2015-04-22 05:17:52
【问题描述】:
如何在 Unity3D 中更改变量仅 5 秒或直到某个事件?例如,改变速度:
void Start ()
{
GetComponent<Rigidbody2D> ().velocity = Vector3.zero;
}
void Update ()
{
if (Input.GetKey(KeyCode.W))
{
goUp ();
}
}
public void goUp() {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 10);
}
自从按下 A 后,物体就一直上升。如何使对象不是一直上升,而是在按下 A 时的每一帧?而当 A 没有被按下时,物体的速度会回到零。我以前是这样弄的:
void Start () {
GetComponent<Rigidbody2D> ().velocity = Vector3.zero;
}
void Update () {
GetComponent<Rigidbody2D> ().velocity = Vector3.zero;
if (Input.GetKey(KeyCode.A)) {
goUp ();
}
}
public void goUp() {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 10);
}
但是这种方法不合理,CPU过载。另外,我不能使用 Input.GetKeyUp(KeyCode.A) 或“else”语句作为触发器。
【问题讨论】: