- Home /
Rotate an object back to its old rotation
Hello all.May be the title is not so clear,so I would like to explain it.I am currently making an aircraft game.The controls are simple,press arrow buttons and rotate it around z axis.I managed to do this.The problem is that,I want the aircraft to return its idle position smoothly if no arrow keys are pressed.I tried some code but it didn't work.I really appreciate any help.Thank you!
Here my code to turn it:
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour {
private float sensitivity = 70;
private float speed = 40;
public float zRotate;
public float backReturn;
private bool rightArrowUp = false;
void Update () {
// change position by arrow keys
transform.position += new Vector3(Input.GetAxis("Horizontal")*speed*Time.deltaTime,0,0);
// change rotation by arrow keys
zRotate -= Input.GetAxis("Horizontal")*sensitivity*Time.deltaTime;
zRotate = Mathf.Clamp(zRotate, -5, 5);
transform.rotation = Quaternion.Euler(0, 0, zRotate);
//rotate back its idle position(0 degrees)
// how????
}
}
this really is a simple question, perhaps you should spend some time in the unity coding manual....
http://docs.unity3d.com/ScriptReference/Transform-rotation.html
http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html
its much quicker to check the manual and you learn quicker if you find the solution for yourself :)
thank you for reply.But actually,I would like to find out the algoirthm.I mean,I want an automatic rotation algorithm to the initial rotation when I stop pressing the arrow keys.
Answer by bubzy · Nov 30, 2014 at 11:12 PM
just wrote this.
Lerp is what you need though, great if you can work this out for yourself, so consider this code a spoiler that you dont have to look at
using UnityEngine;
using System.Collections;
public class rotateThis : MonoBehaviour {
// Use this for initialization
Quaternion originalRotation;
float rotateSpeed = 0.1f;
bool restoreRotation = false;
void Start () {
originalRotation = transform.rotation;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.A)&& !restoreRotation)
{
restoreRotation = true;
}
if(restoreRotation)
{
transform.rotation = Quaternion.Lerp(transform.rotation,originalRotation,Time.time * rotateSpeed);
if(transform.rotation == originalRotation)
{
restoreRotation = false;
}
}
}
}
Hey thank you! I modified it for my purpose but it doesn't work.The code is below:
using UnityEngine;
using System.Collections;
public class CarController : $$anonymous$$onoBehaviour {
private float sensitivity = 70;
private float speed = 40;
public float angle;
public float rotAngle;
public float zRotate;
public float backReturn;
Quaternion originalRotation;
float rotateSpeed = 0.1f;
bool restoreRotation = false;
void Start(){
originalRotation = transform.rotation;
}
void Update () {
transform.position += new Vector3(Input.GetAxis("Horizontal")*speed*Time.deltaTime,0,0);
angle = transform.eulerAngles.z;
if( ( Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.RightArrow) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftArrow) ) && !restoreRotation)
{
restoreRotation = true;
}
if(restoreRotation)
{
transform.rotation = Quaternion.Lerp(transform.rotation,originalRotation,Time.time * rotateSpeed);
if(transform.rotation == originalRotation)
{
restoreRotation = false;
}
}
if(!restoreRotation){
zRotate -= Input.GetAxis("Horizontal")*sensitivity*Time.deltaTime;
zRotate = $$anonymous$$athf.Clamp(zRotate, -5, 5);
transform.rotation = Quaternion.Euler(0, 0, zRotate);
}
}
}