- Home /
 
Smooth 2D rotation unexpected
Hi, I am trying to use smoothDamp to get a smooth 2D rotation going but I can't seem to get the code right... Any help would be very appreciated!
 using UnityEngine;
 using System.Collections;
 
 public class smoothCheck : MonoBehaviour {
 
     public Vector3 diff;
     public float rot_z;
     public float smoothTime = 0.3f;
     private float yVelocity = 0.0f;
     private float newPosition;
 
 
     void Update () {
         Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         diff.Normalize();
         rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
         rot_z = rot_z - 90;
         float current = transform.eulerAngles.z;
         newPosition = Mathf.SmoothDamp(current, rot_z, ref yVelocity, smoothTime);
         transform.rotation = Quaternion.Euler(0.0f, 0.0f, newPosition);
     
     }
 }
 
 
               It seems like the rotation only works in quadrant 2 but not in quadrant 1,3,4. I am not sure what I am doing wrong...
here is an example of what is happening. https://www.youtube.com/watch?v=YDnLRPIksPE&feature=youtu.be
Answer by JinJin · Mar 13, 2015 at 06:24 PM
debug your rotation like this:
 for( int i = 0; i < 360; ++i )
 {
   float x = Mathf.Cos( i*Mathf.Deg2Rad );
   float y = Mathf.Sin( i*Mathf.Deg2Rad );
   rot_z = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
   rot_z = rot_z - 90;
   float current = transform.eulerAngles.z;
   newPosition = Mathf.SmoothDamp(current, rot_z, ref yVelocity, smoothTime);
   Debug.Log( rot_z );
   Debug.Log( current );
   Debug.Log( newPosition );
 }
 
              When I debugged it my game was running very slow and in this script I don't know where to insert the information from the mouse, Screen to world point.
i'm sorry, i should explain in detail :)
use the above code in the Start() or Awake() function, so it executes only 1 time.
this code will simulate rotating the mouse in a full circle of 360°
you will get 360 results in your unity console with the Debug.Log() function
from this 360 results in the unity console, you can see how rot_z, current and newPosition change from 0° to 360° and that should help you find what is wrong
Your answer