How to return original rotation of object
hello i am creating a racing game in which i want my car to move left and right with a little rotation which i have done so far but the problem is when the key is pressed the object rotates but does not come back to original rotation , what i want is that when key is pressed it should rotate but after one second of key pressing it should return to its original rotation. here is the script i have so far.. 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) {
//StartCoroutine (Spin());
car.transform.Translate (Vector3.left);
vehicle.transform.Rotate (Vector3.up, speed * Time.deltaTime);
//vehicle.transform.Rotate(0, 192, 0);
timer -= Time.deltaTime;
timer +=Time.deltaTime;
restorerotation =true;
if( restorerotation && limittimer < timer)
{ // rotation = 1 * inverse;
//rotation *= Time.deltaTime;
//vehicle.transform.Rotate(0, 185, 0);
//vehicle.transform.rotation = originalRotation;
vehicle.transform.Rotate (Vector3.up, -speed * Time.deltaTime);
//transform.rotation = Quaternion.Lerp(transform.rotation,originalRotation,Time.time * rotation);
//transform.Rotate(0, rox, 0);
if(transform.rotation == originalRotation)
{
restorerotation = false;
timer = 0f;
}
}
}
if (Input.GetKey (KeyCode.V)&& !spinning) {
car.transform.Translate (Vector3.right);
//float rotation = 1* rotationspeed;
//float rotation = Time.deltaTime;
vehicle.transform.Rotate (Vector3.up, -speed * Time.deltaTime);
//vehicle.transform.Rotate(0, 192, 0);
timer -= Time.deltaTime;
timer +=Time.deltaTime;
restorerotation =true;
if( restorerotation && limittimer < timer)
{ // rotation = 1 * inverse;
//rotation *= Time.deltaTime;
//vehicle.transform.Rotate(0, 185, 0);
//vehicle.transform.rotation = originalRotation;
vehicle.transform.Rotate (Vector3.up, speed * Time.deltaTime);
if(transform.rotation == originalRotation)
{
restorerotation = false;
timer = 0f;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Fixed Update randomly stops player rotation 0 Answers
Character Rotation 0 Answers
Arrow mechanic like soccer star 0 Answers
player movement not using rotation 0 Answers