Unity Movement - 好的,所以我正在尝试为电梯运动构建一个简单的脚本
Unity Movement - Okay, so I am trying to build a simple script for elevator movement
问题是只有按住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);
}
}
}
我想你可以修改你的代码
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;
}
}
希望对您有所帮助
问题是只有按住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);
}
}
}
我想你可以修改你的代码
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;
}
}
希望对您有所帮助