Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How Quaternion.LookRotation works?

Discussion in 'Scripting' started by Tibor_coder, Oct 10, 2020.

  1. Tibor_coder

    Tibor_coder

    Joined:
    Mar 27, 2020
    Posts:
    26
    Hi, I am trying to make an object rotate towards an other object in 2d. However, I don't fully understand how Quaternion.LookRotation works. I know it asks for an upwards and a forwards vector but I don't know what those are. Could anyone explain to me how Quaternion.LookRotation works?

    I am a beginner in Unity.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The first argument is a vector pointing in the direction you want to look. If you are trying to look at another object, you probably want to use (thatObject.position - thisObject.position).

    The second argument says what direction you want to treat as up. Because theoretically you could still look in the target direction even if you were upside-down or rolled over on your side or something. For 3D, usually you use Vector3.up; but if your 2D game is set in the XY plane then you probably want to use positive Z instead (which is Vector3.forward).
     
  3. Tibor_coder

    Tibor_coder

    Joined:
    Mar 27, 2020
    Posts:
    26
    Hi, thank you for your reply! I am sorry I am a beginner at Unity, so could you explain to me why you use Vector3.forward for 2d games.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It just depends where your ground is. A 3D game usually has the ground on the XZ plane, with gravity being along the negative Y axis. A top-down 2D game often will instead put the ground on the XY plane and assume everything is positioned at Z = 0 all the time.

    Although I suppose it also depends on how you've used the local axes of the object that you are rotating...
     
    Bunny83 likes this.
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Tibor_coder

    You can't use LookRotation for 2D quite like you would use it for 3D. Maybe I don't know all about it, but that is my understanding. See explanation below.

    Quaternion.LookRotation defaults to using z-axis of object as forward. AFAIK you can't change that. For that reason it suits well for 3D objects and these objects will look towards LookRotation's forward direction with their z-axis:
    Code (CSharp):
    1. Quaternion rotation = Quaternion.LookRotation(heading, Vector3.up);
    So for 3D meshes like ship or car this is usually fine if you have created the model along z-axis. However, sprites are usually on XY-plane (not on XZ floor plane), but LookRotation z-axis forward will still be the forward direction for sprites too, i.e. axis that goes through sprite's plane like a pin. This doesn't make much sense. One would think sprite should "look" towards target using its y-axis or x-axis.

    So in this case, if you really want to use LookRotation, you could use Vector3.forward as the first parameter, and then repurpose the upwards vector. This way upwards direction can be used to change sprite's heading (rotation around z-axis, so not actually using it as 3D object's up vector) and "forward" direction is only used as axis of rotation. I've used something like this:
    Code (CSharp):
    1. // direction towards target
    2. var directionVector = (target.position - transform.position).normalized;
    3.  
    4. // your actual heading as upwards parameter
    5. Quaternion lookRotation = Quaternion.LookRotation(Vector3.forward, directionVector);
    6.  
    7. // add offset to rotation if needed, or simply use lookRotation as is
    8. transform.rotation = Quaternion.Euler(0,0, lookRotation.eulerAngles.z);
    9.  
    10. // this will make x-axis of sprite face target instead of y-axis
    11. transform.rotation = Quaternion.Euler(0,0, lookRotation.eulerAngles.z + 90);
    But there are other ways to do this, so you don't have to use Quaternion.LookRotation for sprite heading/angle.

    See:
    https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
     
  6. denioulegein

    denioulegein

    Joined:
    Jan 26, 2024
    Posts:
    1
    Ty dude or master
     
  7. AlexuzZz

    AlexuzZz

    Joined:
    Aug 11, 2023
    Posts:
    11