Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Counting an object's rotations problem

Discussion in 'Scripting' started by ppj34, Jul 2, 2019.

  1. ppj34

    ppj34

    Joined:
    Jul 2, 2019
    Posts:
    1
    Hello,

    I'm looking for a code to count the number of rotations of an object. What I have found so far does not fit my needs. Here is my simple in C# code but that does not work:

    Code (CSharp):
    1.    
    2.     float zCumulRotation = 0.0f;
    3.     float zOldAngle;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         zOldAngle = transform.eulerAngles.z;
    9.     }
    10.  
    11.  // Update is called once per frame
    12.     void Update()
    13.     {
    14.         float z = transform.eulerAngles.z;
    15.         float d = z - zOldAngle;
    16.         zOldAngle = z;
    17.  
    18.         zCumulRotation += d;
    19.         Debug.Log(zCumulRotation);
    20.     }
    The problem seems simple but it did not (for me anyway).

    There is an error when the angle z goes from 0 to 360 or vice versa. As it is a mathematical problem, I can not find a solution.

    Can someone help me please?
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    You will need to have a boolean or two that keeps the state of your counter. For example something like
    Code (CSharp):
    1. bool passedThrough180;
    so everytime your rotation is 180, you set that to true. Then you can use an if statement in your counter to 1) check if that boolean is true and 2) set it to false after you did a count.

    Hope that makes sense?
     
  3. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    Maybe this will help
    Code (CSharp):
    1.  
    2. Update()
    3. {
    4.   angle+= rotationSpeed * Time.deltaTime;
    5.   if (angle > 360f)
    6.   {
    7.     countRot++;
    8.     angle -= 360f;
    9.   }
    10. }
    viceversa
    Code (CSharp):
    1.  
    2. if (angle < -360f)
    3.   {
    4.     countRot++;
    5.     angle += 360f;
    6.   }
    7.  
     
    chubbspet likes this.
  4. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    good suggestion if it doesn't cause jitter. liked...
     
  5. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    But the problem remains .. let's say you're spinning at -200 and then you go up to 200 .. in reality you've made a complete rotation + 40 more degrees.. so you must resolve this problem.
    A simple solution will be:
    You need to detect when the rotation direction changed and reset a dummyAngle variable to 0. So you have the real angle and the dummyAngle who will tell you when you have a full rotation.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I have a simple counting-rotations algorithm near the bottom of this article that handles 360-to-0 crossovers gracefully. As a bonus, the rest of the article is filled with reasons that using eulerAngles for this is going to lead to problems sooner or later, which you should also check out.