Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question How would i get these values in the Transform component?

Discussion in 'Scripting' started by SKULLgaming8777, Sep 4, 2023.

  1. SKULLgaming8777

    SKULLgaming8777

    Joined:
    Dec 11, 2021
    Posts:
    17
    If its possible, how would i go on about getting exactly this value of the object? I tried using euler angles which doesnt unfortunately work. im trying to get this value for a steering wheel and it would make my job much easier.
     

    Attached Files:

  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    140
    If it's only-editor-thing, you can do it by below:
    Code (CSharp):
    1.         void DoSomethingToInspectorRotation()
    2.         {
    3.             var so = new SerializedObject(transform);
    4.             var eulerAngleHint = so.FindProperty("m_LocalEulerAnglesHint").vector3Value;
    5.             // do something...
    6.         }
    There may be no solution in runtime, I think.
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Are you asking how to get the rotation of an object? That's pretty basic stuff, to be fair, unless you're having the issue where it's a child of something, then you'd want
    transform.localRotation
    . But that would give you a Quaternion, and you're probably used to seeing Euler(which shows in the Inspector). So I would research into both of those and get a good grasp on Gimbal Lock, which is the whole reason Unity uses Quaternions instead of Euler.

    But to convert it from Quat to Euler is simply:
    Code (CSharp):
    1. Vector3 quatToEuler = transform.localRotation.eulerAngles;
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,647
    It's important to note the euler angles shown in the inspector are not representative of the euler angles you'll get from that of either the
    transform.rotation
    or
    transform.localRotation
    euler values. They're just there to be human readable, and there's a high chance they will be different from what the values are under the hood.

    This is a case of where you want to seperate the runtime/display values to the actual values you're working off. Just have a float in your script that stores your rotation around this pivot, and when it's updated, rotate the steering wheel with
    Quaternion.AngleAxis
    for example.