团结运动 - 好的,所以我正在尝试为电梯运动建立一个简单的脚本

时间:2015-06-27 04:10:15

标签: unity3d game-physics keyboard-events

问题是如果用户持有E键,我只能向上移动。有没有办法让用户按下E键然后才启动电梯?

这是我的代码:

using UnityEngine;
using System.Collections;

public class liftScript : MonoBehaviour {

    public int speed = 1;
    private int i = 10;


    void OnTriggerStay()
    {
        startLift ();
    }

    void startLift()
    {
        if(Input.GetKey(KeyCode.E)) {
            transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x, 10, transform.position.z), Time.deltaTime * speed);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我认为你可以修改你的代码

public class liftScript : MonoBehaviour {

public int speed = 1;
private int i = 10;
bool keyPressed = false;

// Update is called once per frame
void Update () {
    if (Input.GetKey (KeyCode.E)) {
        keyPressed = true;
    }

    if (keyPressed == true)
    {
        startLift();
    }

}

void startLift()
{
        transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x, 10, transform.position.z), Time.deltaTime * speed);
}

void stopLift()
{
    keyPressed = false;
}
}  

希望它可以帮到你