Search Unity

Need help with Quaternion LookRotation

Discussion in 'Scripting' started by Shadowing, Sep 11, 2019.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,234
    My character controller has a method called move with arguments

    /// <param name="horizontalMovement">-1 to 1 value specifying the amount of horizontal movement.</param>
    /// <param name="forwardMovement">-1 to 1 value specifying the amount of forward movement.</param>
    /// <param name="lookRotation">The direction the character should look or move relative to.</param>

    Code (csharp):
    1. Move(float horizontalMovement, float forwardMovement, Quaternion lookRotation)
    I need to make the controller look at another transformer.

    i only know how to do it with
    Code (csharp):
    1. transform.LookAt(targetPostition);
    wondering if anyone could help me out with this.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,511
    transform.LookAt(targetPosition) is identical to:
    Code (csharp):
    1. transform.rotation = Quaternion.LookRotation(targetPosition - transform.position);
    Does that help?
     
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,234
    Thanks @StarManta that was exactly what I was looking for.
     
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,234
    Actually I need help with something else thats on the same topic.
    My character will be standing on top of NextDestination. I'm using LookAt so I need to find a spot in the distance for him to look at. I want to use the local blue transform arrow of NextDestination

    Tried several things. This is what I have atm. Still isn't pointing the right way though.
    If i keep it all in world space it works though.


    Code (csharp):
    1.  
    2. Vector3 ForwardPosition =  NextDestination.localPosition + Vector3.forward * 10;
    3.                 .Move(0,0,Quaternion.LookRotation(transform.TransformPoint(ForwardPosition) - transform.position));
    4.  
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,072
    If you want a Quaternion that looks in the same direction as the local blue axis of "NextDestination", I believe you can just use
    Quaternion.LookRotation(NextDestination.forward)
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,234