此代码是这样的,当我按A键或B键时,电梯在第一层或第二层向上停止。
问题是
例如,当电梯位于二楼时,如何修改代码,然后再次按A键使电梯下降到一楼?
第二个问题
如果我替换
if (Input.GetKey (KeyCode.A)) {}
与此
if (Input.GetKeyUp (KeyCode.Keypad1)) {}
所以该代码不起作用。为什么?
谢谢您的建议
对不起,英语不好。这是代码
public GameObject lift;
private bool keyHHit=false;
private bool keyHHitB=false;
void Update ()
{
if (Input.GetKey(KeyCode.B))
{
keyHHit=true;
}
if( keyHHit==true)
{
if(transform.localPosition.y >= 14.52)
{
transform.Translate(new Vector3(0, 0, 0) * 2 * Time.deltaTime, Space.Self);
}
else
{
transform.Translate(new Vector3(0, 2, 0) * 2 * Time.deltaTime, Space.Self);
}
}
if (Input.GetKey(KeyCode.A))
{
keyHHitB=true;
}
if( keyHHitB==true)
{
if(transform.localPosition.y >= 8.52)
{
transform.Translate(new Vector3(0, 0, 0) * 2 * Time.deltaTime, Space.Self);
}
else
{
transform.Translate(new Vector3(0, 2, 0) * 2 * Time.deltaTime, Space.Self);
//transform.Translate(Vector3.up * 0.05f);
}
}
}
所以,如果我能正确理解你的话
您有两层或三层(没关系,以后可能会再多)。
一旦按下按钮,您想朝目标楼层移动直到到达那里。同时“锁定”输入
一旦到达楼层“解锁”,再次输入,并且如果当前楼层高于目标楼层,也允许其下降。>
我将为此使用Coroutine
// First lets have a class for floors so it is easier to add additional floors later [Serializable] public class FloorSetting { public KeyCode Key; public Vector3 Position; } // Configure these floors in the Inspector // simply add the entries you need and adjust buttons and target positions [SerializeField] private FloorSetting[] floors; [SerializeField] private float moveUnitsPerSecond = 1f; // This flag is for blocking any Input while moving // thus preventing concurrent routines private bool isMoving; private void Update() { // if already moving do nothing if(isMoving) return; // check if any of the configured keys was pressed and start moving // towards the according position foreach(var floor in floors) { // You want to use GetKeyDown to only get the first press event // you don't care about continues press since you will keep moving automatically until reaching the floor if(Input.GetKeyDown(floor.Key)) { StartCoroutine(MoveRoutine(floor.Position)); } } } private IEnumerator MoveRoutine(Vector3 targetPosition) { // block concurrent routine if(isMoving) yield break; isMoving = true; // Get the duration of movement var startPosition = transform.position; var duration = Vector3.Distance(startPosition, targetPosition) / moveUnitsPerSecond; // Move smoothly towards the target position and add some easing var timePassed = 0f; while(timePassed <= duration) { // A interpolation factor between 0 and 1 var factor = timePassed / duration; // optionally add ease-in and out factor = Mathf.SmoothStep(0, 1, factor); // Set the position to an interpolated position between start and target depending on the factor transform.position = Vector3.Lerp(startPosition, targetPosition, factor); // increase by the time passed since last frame timePassed += Time.deltaTime; // Tells Unity to "pause" the routine here, render this frame // and continue from here in the next frame yield return null; } // Just to be sure in he end set it to hard position transform.position = targetPosition; // optionally add some cooldown in seconds yield return new WaitForSeconds(1f); // Release the lock so next move can start now isMoving = false; }
注:在智能手机上输入,但我希望这个主意能清楚明白