2017-06-12 68 views
0

我正在创建一个赛车游戏,我希望我的车左右移动一点旋转 这是我迄今为止所做的,但问题是当键是按下物体旋转,但 不会回到原来的旋转,我想要的是,当按下键时,它应该旋转,但按下按键一秒后,它应该返回到其原始旋转。 这里是剧本我至今..返回对象旋转后的x时间量的原始旋转

using UnityEngine; 
using System.Collections; 

public class CarKeyboardMovement : MonoBehaviour { 

    public GameObject car; 
    public GameObject vehicle; 
    public float turnSpeed = 20f; 

    private bool spinning = false; 


    public float speed = 20; 

    Quaternion originalRotation; 

    public bool restorerotation = false; 
    public float timer = 0.0f ; 
    public float xtimer = 0.0f; 
    public float limittimer = 1f; 


    void Start() {  
     originalRotation = vehicle.transform.rotation;  

    } 

    // Update is called once per frame 
    void Update() { 
     if (Input.GetKey (KeyCode.Z) && !spinning) { 
      car.transform.Translate (Vector3.left);    

      vehicle.transform.Rotate (Vector3.up, speed * Time.deltaTime); 
      timer -= Time.deltaTime; 
      timer +=Time.deltaTime; 
      restorerotation =true; 
      if(restorerotation && limittimer < timer) 
      { 
       vehicle.transform.Rotate (Vector3.up, -speed * Time.deltaTime); 

       if(transform.rotation == originalRotation) 
       { 
        restorerotation = false; 
        timer = 0f; 
       } 
      } 
     } 
     if (Input.GetKey (KeyCode.V)&& !spinning) { 

      car.transform.Translate (Vector3.right); 
      vehicle.transform.Rotate (Vector3.up, -speed * Time.deltaTime); 
      timer -= Time.deltaTime; 
      timer +=Time.deltaTime; 
      restorerotation =true; 
      if(restorerotation && limittimer < timer) 
      { 
       vehicle.transform.Rotate (Vector3.up, speed * Time.deltaTime); 

       if(transform.rotation == originalRotation) 
       { 
        restorerotation = false; 
        timer = 0f; 

       } 
      } 
     } 
    } 

回答

0

你的代码中有多个逻辑错误,这些都需要加以固定。这是我在查看代码约45秒后注意到的。

spinning是无所事事的你,它总是假的(你永远不会设置spinning为true),所以!spinning始终是真实的。 condition && true简化为condition

limittimer同样无所事事的你,你永远不其值设置为大于1同样timer以外的任何始终是0:将其设置为0,则每帧而输入收到你添加然后的DeltaTime:

 timer -= Time.deltaTime; 
     timer += Time.deltaTime; 

所以timer的价值不会改变,并limittimer < timer1 < 0)始终是假的。

但是它并不重要,因为此代码(它应该在回到原始旋转后阻止对象旋转)不会被称为,除非用户正在按特定覆盖的键 “返回原来的”行为。

xtimer从来没有使用过。