# How Quaternion.LookRotation works?

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.

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).

5 Likes

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.

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…

1 Like

@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:

``````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:

``````// direction towards target
var directionVector = (target.position - transform.position).normalized;

Quaternion lookRotation = Quaternion.LookRotation(Vector3.forward, directionVector);

// add offset to rotation if needed, or simply use lookRotation as is
transform.rotation = Quaternion.Euler(0,0, lookRotation.eulerAngles.z);

// this will make x-axis of sprite face target instead of y-axis
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:

6 Likes

Ty dude or master

Documentation is quite descriptive: Unity - Scripting API: Quaternion.LookRotation

Also, try to watch this video -