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

Rotation Sign Change

Discussion in 'Scripting' started by BradMick, Feb 14, 2019.

  1. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Howdy all,

    I've hit a bit of a snag with rotations in unity and I've not been able to find a solution looking through the forums of programmers guides. What I need is for the rotation to remain negative when rotating counter clockwise and positive with rotating clockwise. What i've observed in Unity is that when rotating counter-clockwise the rotation output will remain negative until it reaches 90 degrees from its start position and then flip to positive. I need it to remain negative through the full 360 degrees or else it throws of the calculations that rely on it off.

    If i've read and understand the rotations docs correctly, this behaviour is as intended, since the underlying values range from -1 to 1? but I need to change that behavior. Here's a screen shot showing what I'm trying to do. Here's a series of screenshots showing my objects (it's a rotor blade) progression from where it starts on the advancing side to over the nose and retreating side. The yellow debug line is correct on the advancing side, but not how it reverses in the second screen shot, which isn't what I want or need because it ends up reversing the the lift vector and everything else...which is bad. But then it reverse again over the tail.

    So yup, how do I prevent the sign change during rotation? Any and all help would be greatly appreciated!

    V/R

    BradMick

    upload_2019-2-14_6-28-11.png upload_2019-2-14_6-30-28.png upload_2019-2-14_6-32-41.png
    upload_2019-2-14_6-33-14.png
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    There isn't a way to override Unity's base rotation behaviour. Unity works primarily with Quarternions. When you give Unity a Vector3 Euler angle to rotate from, it will convert this into a Quaternion behind the scenes, perform the translations/calculations, then display euler angles for the new rotation afterwards. It's possible to put in -760 degrees in the Inspector as it's working as a human readable tool and doesn't necessarily represent the true numbers kept behind the scenes.

    However you can still totally make your own number that represents the positive/negative offset from it's original rotation from the start of play. Then use that for your calculations instead.

    Code (CSharp):
    1. public class Example : MonoBehaviour
    2.     {
    3.         // Vector3 applies to any transform and isn't stuck to the -1,+1 of euler angle rotations. This will store the positive/negative values correctly.
    4.         private Vector3 offset = Vector3.zero;
    5.      
    6.         // Whenever you rotate make sure you use this method, so the offset is always correct.
    7.         public void Rotate(Vector3 amount)
    8.         {
    9.             offset += amount;
    10.             transform.Rotate(amount), Space.Self);
    11.         }
    12.  
    13.         // Get the rotation from this method, to make sure you never accidentally change the offset in some other class by mistake.
    14.         public Vector3 HowMuchRotatedSoFar()
    15.         {
    16.             // We know this is whatever has rotated so far.
    17.             return offset;
    18.         }
    19.     }
     
  3. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Awesome! Thank you very much. Works like a champ!

    Now here's my other question...in further investigating what's going on, It looks like it's the actual vector system that's causing the issue. If I run my own custom trig functions all the math for the rotating blade checks out. Whenever I try and do this with Unity's vectors...not so much.

    upload_2019-2-15_9-47-24.png upload_2019-2-15_9-47-41.png

    The two screengrabs illustrate prior to crossing the z-axis and after. In the inspector, you'll find AoA...AoA per-crossing the Z axis is correct, wheras post crossing the Z axis it is not. I'm starting to suspect it isn't so much the rotation that's causing the issue as it's the Vector functions under the hood causing the issue. As I said above, when I run the numbers as straight trig functions things are clean and work as desired, when I use Vectors things break on crossing axes.

    I'd like to use a vector solution because it's easier all around, but I definitely need some help solving the z axis flip that i'm running into.

    V/R

    BradMick
     
  4. da_egg5

    da_egg5

    Joined:
    Nov 20, 2021
    Posts:
    8
    I don’t get the last method, how is it returning the offset when there is no valuations going on in that code block
     
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Really it's just returning the value of how much has been rotated so far, which is done in the other method. It could happily be a property instead:

    Code (CSharp):
    1. public class Example : MonoBehaviour
    2.     {
    3.         // Vector3 applies to any transform and isn't stuck to the -1,+1 of euler angle rotations. This will store the positive/negative values correctly.
    4.         private Vector3 offset = Vector3.zero;
    5.  
    6.         // Get the rotation from this method, to make sure you never accidentally change the offset in some other class by mistake.
    7.         public Vector3 Offset => offset;
    8.  
    9.         // Whenever you rotate make sure you use this method, so the offset is always correct.
    10.         public void Rotate(Vector3 amount)
    11.         {
    12.             offset += amount;
    13.             transform.Rotate(amount, Space.Self);
    14.         }
    15.     }
     
  6. da_egg5

    da_egg5

    Joined:
    Nov 20, 2021
    Posts:
    8
    After staring at it a little it made total sense, and was very useful! Thank you!