Track object rotation

Hi.

I am rotating an object in FixedUpdate()around Z axis. Now I need to track that object’s rotation and store it in float rot variable.

Problem is that i am getting rot to be from 0 to 360. Is it possible to extend that and get it to be higher hen 360?

For example, when object is rotated for 520 degrees i get rot=520.

This kind of problem can be ugly to solve. Here is some example of one way to approach the probelm. ‘rotationAmount’ will be the positive or negative rotation from the original rotation.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {

	private float angle = 0.0f;
	private float rotationAmount = 0.0f;

	void Update() {
		Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint (transform.position);
		float newAngle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
		rotationAmount += Mathf.DeltaAngle (angle, newAngle);
		angle = newAngle;
		transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
		Debug.Log (rotationAmount);
	}
}

This code assumes the initial rotation of the object is considered 0.0. In addition since ‘rotationAmount’ is calculated by adding lots of relative rotations, a small error will creep in due to floating point imprecision. Note that I’m converting the transform.position to a screen coordinate rather than the other way around like your code. The reason is that you code will only work (as is) for an Orthographic camera. This code will work for perspective as well.

You can divide by 360 and take the floor to get the number of positive or negative rotations if all you want is the count of rotations.

Final note. Avoid direct comparisons of floating point numbers as in this line:

if(angle==360) angle=0;

There is nothing internal that has that information saved, so you have to calculate it based on how many full rotations you’ve made previously.

Create a variable that saves the rotation from the previous frame and one that saves the rotation from the current frame, then if there difference is say, more than 270 add to your full rotations variable, if it is less than -270, minus from your full rotations variable!

Then when last frame the angle was near 0 and then it became near 360, you minus a full rotation, and vice versa.

Then you can get your rotation by doing rotation = 360*fullRotations + currentRotation;

Scribe

EDIT: based on your code you don’t allow your object to rotate more than 6 degrees every frame so there would be no problem with this method, as this method only breaks if you rotate more than 270 degrees in a frame. Here’s how it might look with your code:

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;
}