Search Unity

[Solved] Yet Another LookAt Question (already rotated nested objects rotating)

Discussion in 'Scripting' started by par-002, Jan 25, 2019.

  1. par-002

    par-002

    Joined:
    Jul 4, 2012
    Posts:
    52
    I shall preface this with a big old "I'M SORRY". This question has been asked/answered so many times that I should be able to figure it out but I am going nuts trying to solve it.

    I have one object that needs to "LookAt" another object with respect to only the Y axis. Simple enough.

    I have a GameObject "Container" that holds another object as a child which (of course) has its own object as a child. Only the "Container" object is rotated 90 degrees on the X axis. Its the child's child I need to do the LookAt-ing to something that is somewhere else in world space BUT, I need its rotation represented in the Y axis of World Space.

    Think of a top down game where sprites are layered on top of each other. But the only way to get the sprites to be viewed by the camera, their original placement needs to be rotated by 90 degrees on the X axis.

    Hierarchical Representation:

    WorldSpace
    |----->GameObject "Container" has a rotation = (90, 0, 0)
    |---------->GameObject "FirstChild" has a rotation = (0, 0, 0)
    |--------------->GameObject "FirstChild's Child" has a rotation = (0, 0, 0)

    |----->GameObject "Target" has a rotation = (0, 0, 0).

    So, the object "FirstChild's Child" needs to rotate every update towards the object "Target". "Target" is the root of that hierarchy where "Container" is the root of the "FirstChild's Child" object.

    When I use the typical Quaternion.LookRotation() rotation or Atan2 + Quaternion.AngleAxis() methods, I get the actual object that needs to rotate facing is so many different directions my head is spinning.

    Any help would be greatly appreciated!

    Edit #1:

    So the only thing I was able to get to work was to do the following:

    Code (CSharp):
    1.  
    2. transform.LookAt(targetBeingTracked.transform.position);
    3. transform.rotation *= Quaternion.Euler(new Vector3(90, 0, 0));
    4.  
    Unfortunately I have to rotate the direction first and then rotate around the axis when that is done in order to get the "FirstChild's Child" object to be on the correct face. I know of no way to Lerp this.

    Edit #2:

    Figured it out. The real problem is working 55 hours in a week and then hitting this stuff on a Friday while trying to get the wife to push dinner off another hour. Sigh...

    Anyway, if anyone searches for this, the main thing that tripped me up was thinking the parent rotation was a big deal. Its' not. Just make up for it in a Quaternion variable and THEN Lerp it:

    Code (CSharp):
    1. Vector3 relativePos = targetBeingTracked.transform.position - transform.position;
    2. Quaternion rotation = Quaternion.LookRotation(relativePos);
    3. rotation *= Quaternion.Euler(new Vector3(90, 0, 0));
    4.  
    5. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * speed);
    Cheers!
     
    Last edited: Jan 25, 2019