Please explain transform.LookAt(transform.position + direction);

hi guys:

is there a tutorial explain all the necessary maths for unity?
i searched around and find a good one explaining the below:

Vector3 dirToCamera = (mainCamera.position - transform.position).normalized;
which is direction = destination - source and why it need to be normailized.

but nothing on why below?
transform.LookAt(transform.position + dirToCamera * -1);

as oppose to just:
and what this pointing at ? i tried to run the 2nd one seems like the text is pointing at the camera with y axis. instead of z axis.

1 Like

Hello welcome to the community!

Here’s a math course by gamedevtv:

Here’s the answers to your questions:

When normalized, a vector keeps the same direction but its length is 1.0.

you can edit this in Unity buy looking at the transform, out using Cinemachine

Hope this helps!

My main queetion is why we need to add transform.position ?

I suppose there are 2 kinds of vector3: position vector and direction vector?

for the LookAt(transform.position + direciton) case i suppose the transform.position provides the up down left and driection is for direction?

whats would happened if we just use LookAt(direction)?
Like the 1st pic what’s happening here? Could you explain? the first one is ran with LookAt(direction).

1 Like

We normalize the vector because we want to be very precise about the location that we’re looking at. Technically, we may not need to normalize it for this next line:

This creates a point past the unit exactly one unit on the other side of the camera.

dirToCamera is a direction. LookAt treats the Vector3 as a physical point in space, meaning that instead of looking away from the Camera, we’re looking somewhere near the tile at 0,0.

1 Like

right. thanks a lot Brian. So without a transition.position it can be any point + that direction. i get it now.

1 Like

I’ll throw in my 2 cents. A vector just holds a point in space. The direction is from 0,0 (the origin in 2D) to that point. What we want is for the direction to be from transform.position instead. So, we translate (move) the origin by adding transform.position to it.

Here’s a quick representation

If we are standing at 2,1 and we do not translate the origin, we will be looking at the wrong place. We add the 2,1 position in order to fix that error.

For 3D it’s exactly the same, except that we now have a third component - the Z-axis

1 Like

thanks a lot

1 Like

Privacy & Terms