Search Unity

How to set child object world space rotation

Discussion in 'Entity Component System' started by MasoInar, Oct 5, 2019.

  1. MasoInar

    MasoInar

    Joined:
    Feb 20, 2014
    Posts:
    126
    Hi, I have a bit problem setting child object rotation. It looks like setting Rotation component value affects only local space rotation.

    My entity looks like 'Body -> Child' and I'm trying to rotate Child object towards target. It works fine as long as I don't rotate my Body object. I tried to look documents and there are LocalToParent and LocalToWorld components, but couldn't find a way to set child object rotation in world space using those..

    Maybe someone could help me or point me in right direction or has some example code?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    LTW is what you need, it represents object transformation in world space, here is couple of things that you can do.
    1. Setup rotation (in radians) directly in to LTW matrix.
    2. “mul” your rotation quaternion in world space with inverted LTW and setup in to Rotation component.
    etc.
     
    MasoInar likes this.
  3. MasoInar

    MasoInar

    Joined:
    Feb 20, 2014
    Posts:
    126
    I ended up doing the second way. I multiple child rotation by parent object inverse rotation and seems to work perfectly!
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    If will be more than one parent you should do it through hierarchy...no good. With mul to inverted LTW you should do it once.
     
  5. MasoInar

    MasoInar

    Joined:
    Feb 20, 2014
    Posts:
    126
    Somehow I couldn't get it to work properly when I tried using inverted rotation from LTW. Do you know if there's any example codes somewhere?
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Oh sorry for confusing you, It was morning for me :D It's rotation, you can't easily mul it from world to local like translation, yeah solution with mul quaternions through hierarchy should work.