- Home /
 
Smooth reset rotation problem
Hi, I want make a very very simple smooth reset rotation function. To test it I just make a simple Cube and then attach this script to it. I also make a simple Empty game object with zero rotation as the target rotation.
What I expect is if after I rotate the Cube by moving the mouse while pressing the right button, then I press the "r" key on keyboard, the cube rotate back smoothly to it's original rotation.
But what I get is I must press the "r" key repeatedly 'till the Cube reach zero rotation.
Someone could help me to fix this script pleasse?
 using UnityEngine;
 using System.Collections;
 
 public class cameraRotation : MonoBehaviour {
 
     public float     turnSpeed     = 300f;
     public float     smooth         = 100f;
     public Transform     target;
 
     private Transform     myTransform;
 
     void Awake()
     {
         myTransform = transform;
     }
 
     void Update()
     {
         if(Input.GetMouseButton(1))
         {
             myTransform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime, 0));
         }
         
         if(Input.GetKeyDown("r"))
         {
             float step = smooth * Time.deltaTime;
             transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step);
         }
     }
 }
 
              Answer by jenci1990 · Dec 11, 2014 at 07:15 PM
Add this function to the coroutine:
 using UnityEngine;
 using System.Collections;
     
 public class cameraRotation : MonoBehaviour {
     public float turnSpeed = 300f;
     public float smooth = 100f;
     public Transform target;
     private Transform myTransform;
     private bool isReset;
     
     void Awake() {
         myTransform = transform;
     }
     
     void Update() {
         if (Input.GetMouseButton(1) && !isReset) {
             myTransform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime, 0));
         }
         if (Input.GetKeyDown("r")) {    
             StartCoroutine(Reset());
         }
     }
     
     IEnumerator Reset() {
         if (isReset) yield break;
         isReset = true;
         float timer = 0f;
         Quaternion startRot = transform.rotation;
         while (timer <= 1f) {
             timer += Time.deltaTime * smooth/100f;
             transform.rotation = Quaternion.Lerp(startRot, target.rotation, timer);
             yield return new WaitForEndOfFrame();
         }
         Debug.Log(timer);
         transform.rotation = target.rotation;
         isReset = false;
         yield break;
     }
 }
 
              Its work !! Thankyou very very much...! my problem solved!! :D
Your answer
 
             Follow this Question
Related Questions
Choppy rotation of character around y-axis 1 Answer
Rotation - Simple Question 0 Answers
smooth look at with offset 0 Answers
Quaternion Rotation Smooth 1 Answer
Quaternion snaps to a rotation when moving (and with input)? 1 Answer