Search code examples
unity-game-enginerotationanglegameobject

Unity track object rotation


I have several object rotating by itself in FixedUpdate().
Now I need to track rotation of one object, lets call it objX. Rotation goes only from 0 to 360 when I retrieve it. Is it possible to get rotation after 360 degrees?
For example when I use something like

float x = objX.transform.rotation.z;

variable x should be 560 degrees.
Is something like that possible?


Solution

  • Here is the way to track rotation.

    Vector3 mouseClickPos;
        float angle;
        float lastAngle;
        int fullRotations;
        public float realAngle;
    
        void FixedUpdate(){
            mouseClickPos = Input.mousePosition;    
            Vector3 dir = mouseClickPos - Camera.main.WorldToScreenPoint(transform.position);
            angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
            angle-=90;
            if(lastAngle - angle > 270){
                fullRotations ++;
            }else if(angle - lastAngle > 270){
                fullRotations --;
            }
            Debug.Log(360*fullRotations + angle);
            Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 6);
            lastAngle = angle;
        }