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. Dismiss Notice

how to i convert a local rotation to a global rotation for the object?

Discussion in 'Scripting' started by creat327, May 10, 2017.

  1. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,739
    Hi

    I need to use Rigidbody.rotation which seems to take only a global rotation.
    The problem is that the value I get from other functions I'm using is the local rotation of the object, so I need to convert that local rotation quaternion into the corresponding global rotation to pass to the rigidbody.rotation as a parameter.

    It must be simple but i'm failing badly so far
     
    ModLunar and MarleDavid like this.
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
  3. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,739
    yeah, I googled and found that one too but it didn't work. Not sure if it was my fault or the way to do it on there is wrong. In my case also I don't have a "parent" to compare to, because my object is the parent itself.
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    If the object has no parent, then the local and world rotation is the same.
     
    harryblack and creat327 like this.
  5. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,739
    doh... I hide into a hole. Brain fart.
     
    marcAKAmarc and harryblack like this.
  6. A_Gamgie

    A_Gamgie

    Joined:
    Mar 30, 2013
    Posts:
    9
    Hello all,

    I am stuck in this problem that seems pretty obvious and common. I googled it a lot and all I tried is not working.

    I am trying to convert my local rotation to world rotation to apply it to rigidbody. Whatever I try, I always end with rotation around world axis.

    This line rotate around world axis.
    _rigidbody.rotation = Quaternion.Euler(cameraRotation);

    I want to convert cameraRotation (Vector3 euler angle local rotation) to world rotation.

    Any tips ?

    Tanks !
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,894
    .rotation
    is world rotation. If you want to mutate it's local rotation, use
    .localRotation
    .
     
  8. A_Gamgie

    A_Gamgie

    Joined:
    Mar 30, 2013
    Posts:
    9
    There is no .localRotation for a rigibody. If you move your transform directly with a non kinematic rigidbody object. It is gonna get messy.
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,894
    Well local rotation can be viewed as the rotation - parent.rotation (
    transform.rotation * Quaternion.Inverse(parent.rotation
    ). So if you want to 'set' a local rotation of a rigibody, you can generate a world rotation by combining your 'local' rotation and the rotation of the parent, and applying that to the rigidbody.