# Counting an object's rotations?

Discussion in 'Scripting' started by bigkahuna, Mar 4, 2007.

Not open for further replies.
1. ### bigkahuna

Joined:
Apr 30, 2006
Posts:
5,434
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?

Joined:
Nov 15, 2006
Posts:
740
You can count the # of inversions instead of 0s perhaps?

3. ### StarManta

Joined:
Oct 23, 2006
Posts:
8,777
I'd suggest calculating the difference between the angle from frame to frame, and adding it together. That total / 360 = number of rotations.

4. ### bigkahuna

Joined:
Apr 30, 2006
Posts:
5,434
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!

5. ### Omar Rojo

Joined:
Jan 4, 2007
Posts:
494
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.

Code (csharp):
1.
2. var X = 0.0;
3.
4. private var T = 0.0;
5.
6. function Update ()
7. {
8.    transform.Rotate (0, X, 0);
9.    T += X;
10.
11.    Debug.Log ("Rotations: " + (T */ 360.0));
12. }
13.

6. ### bigkahuna

Joined:
Apr 30, 2006
Posts:
5,434
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!

7. ### Talzor

Joined:
May 30, 2006
Posts:
197
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.

Code (csharp):
1.
2. using UnityEngine;
3. using System.Collections;
4.
5. public class CountRotations : MonoBehaviour {
6.     private float totalRotation = 0;
7.     public int nrOfRotations {
8.         get {
9.             return ((int) totalRotation)/360;
10.         }
11.     }
12.
13.     private Vector3 lastPoint;
14.
15.     // Use this for initialization
16.     void Start () {
17.         lastPoint = transform.TransformDirection(Vector3.forward);
18.         lastPoint.y = 0;
19.     }
20.
21.     // Update is called once per frame
22.     void Update () {
23.         Vector3 facing = transform.TransformDirection(Vector3.forward);
24.         facing.y = 0;
25.
26.         float angle = Vector3.Angle(lastPoint, facing);
27.         if (Vector3.Cross(lastPoint, facing).y < 0)
28.             angle *= -1;
29.
30.         totalRotation += angle;
31.         lastPoint = facing;
32.     }
33. }
34.

Vicky4455 and KalyanKP like this.

Joined:
May 30, 2006
Posts:
197
Ah, to slow

9. ### bigkahuna

Joined:
Apr 30, 2006
Posts:
5,434
Oooo... Talzor, your code is sooo much prettier than mine! Mine is held together with band-aids and scotchtape by comparision!

Thanks, I'll give it a try!

Joined:
Jan 27, 2011
Posts:
21

11. ### Ryan_BuzzInteractive

Joined:
Jan 25, 2017
Posts:
1

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:

Code (csharp):
1.
2. Vector3 facing = transform.parent.InverseTransformDirection(transform.forward);
3.
Cheers!

Last edited: Jan 26, 2017
12. ### EddieChristian

Joined:
Oct 9, 2012
Posts:
725
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.

Alverik likes this.
13. ### Ami_2007

Joined:
Feb 2, 2022
Posts:
1
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

Joined:
Apr 7, 2021
Posts:
1
Thank you.

Joined:
May 24, 2013
Posts:
11,683