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;
}
}
}
}