# Counting an object's rotations?

I’m trying to come up with a bit of Javascript that counts the number of rotations for and object.

I tried using “transform.Rotation” and “transform.EulerAngle”, but Rotation resets itself when it reaches a second rotation and EulerAngle inverts when it passes 0 (zero).

I thought about counting the number of times my object’s rotation passes 0 (zero), but I’m afraid that if 0 is passed too quickly it won’t be picked up by the script.

Any other ideas or suggestions?

You can count the # of inversions instead of 0s perhaps?

I’d suggest calculating the difference between the angle from frame to frame, and adding it together. That total / 360 = number of rotations.

I probably didn’t explain this well enough, but the object can rotate in either direction around it’s Y axis. Imagine a “worm gear” and it can turn just so many times before reaching the end of the gear. So none of my original ideas would have worked. :?

Thanks Craig - “Inversions”? I’m not sure what you mean, but I’ll see what I can find.

Thanks StarManta - I don’t think that will work because the object can rotate in either direction.

Thanks guys, keep the ideas coming!

EDIT: Got it running, thanks!

Maybe this was posted before but it seems logical to me that if on a function you rotate an object by an angle X and if you have a variable T that adds X in the same function, T will always have the total rotation, for X positive and negative.

Simple.

``````var X = 0.0;

private var T = 0.0;

function Update ()
{
transform.Rotate (0, X, 0);
T += X;

Debug.Log ("Rotations: " + (T */ 360.0));
}
``````

Thanks Omar. I see what you’re suggesting. Actually I’m using rotary forces to turn the worm gear and not directy rotating it, so I needed to track the object’s rotation as a second function. (does that make sense?) If I understand what your code is doing, I don’t think that’s what I was looking for.

The good news is that after spending an entire day on this, I finally got a piece of code together that worked. It ain’t pretty, but so far it seems to work.

Thanks again!

He’s probably rotating it in ways that doesn’t give him any easy reference (e.g. with physics)

@bigkahuna. Are you looking for something like this?
Note that this has some limitations. It assumes that the object is only rotating in Y and that the rotation is no more than 180 degress in either direction pr. frame.

``````using UnityEngine;
using System.Collections;

public class CountRotations : MonoBehaviour {
private float totalRotation = 0;
public int nrOfRotations {
get {
return ((int) totalRotation)/360;
}
}

private Vector3 lastPoint;

// Use this for initialization
void Start () {
lastPoint = transform.TransformDirection(Vector3.forward);
lastPoint.y = 0;
}

// Update is called once per frame
void Update () {
Vector3 facing = transform.TransformDirection(Vector3.forward);
facing.y = 0;

float angle = Vector3.Angle(lastPoint, facing);
if (Vector3.Cross(lastPoint, facing).y < 0)
angle *= -1;

totalRotation += angle;
lastPoint = facing;
}
}
``````
2 Likes

Ah, to slow

Oooo… Talzor, your code is sooo much prettier than mine! Mine is held together with band-aids and scotchtape by comparision! :lol:

Thanks, I’ll give it a try!

Thanks for that code, it really helped me get going. I did find that if I rotated my object’s parent then the calculations all went off so I needed to get my object’s forward vector in local space (replacing lines 17 and 23 in your example) like this:

``````Vector3 facing = transform.parent.InverseTransformDirection(transform.forward);
``````

Cheers!

This script it the closest I have come to finding a solution to what I need. I’m trying to get the values of the Variables TotalRotation and nrOfRotations but in all 3 Local Axis. But not sure how to modify this to get all 3. It’s for a script to limit rotations on all 3 axis.

1 Like

If you want to count rotations on both sides just add Mathf.Abs to always get positive angle

totalRotation += Mathf.Abs(angle);

On line 30

Thank you.