2017-06-12 5 views
0

私は車を少し回転させて左右に動かすレーシングゲームを作成しています これまで行ってきましたが問題はキーがオブジェクトは回転しますが、 は元の回転に戻らないので、キーを押したときに回転しますが、キーを1秒間押すと元の回転に戻ります。 はここ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は何もしていませんが、常にfalse(spinningをtrueに設定しないでください)の場合、!spinningは常にtrueです。 condition && trueconditionに簡素化されます。

limittimerは、同様に、あなたのために何もしないで、あなたは同様に1 timer以外に、その値を設定することはありませんが、常に0である:あなたが入力が受け取っている間、その後、すべてのフレームは、あなたがその後、減算を追加0に設定し deltatime:

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

のでtimerの値が変化しないとlimittimer < timer1 < 0)は常にfalseです。

しかし、このコード(オブジェクトが元の回転に戻った後にオブジェクトの回転を停止するはずです)は、ユーザーがのキーを押していない限り、とは呼ばれません「元に戻す」動作。

xtimerはまったく使用されません。

関連する問題