私は車を少し回転させて左右に動かすレーシングゲームを作成しています これまで行ってきましたが問題はキーがオブジェクトは回転しますが、 は元の回転に戻らないので、キーを押したときに回転しますが、キーを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;
}
}
}
}