[Solved] LookRotation is passed a non-normalized vector in the lecture code

In this lecture (Rotate to Face Target), the vector “direction” is normalized.

Then, the LookRotation method is passed new Vector3(direction.x, 0, direction.z).

But, if the player and enemy are at different height levels, this vector passed to LookRotation is not normalized (though that wouldn’t be noticed in the example where they both move on a horizontal plane). If I remember right, LookRotation (in a Debug context, such as playing in editor) throws an exception if it gets a vector of length not equal to 1.

Thus, I used the following code instead:

        Vector3 direction = target.position - transform.position;
        direction.y = 0;
        transform.rotation = Quaternion.LookRotation(direction.normalized);
2 Likes

Hi Todd,

What was your solution?

The code above.

Thanks for sharing your solution. :slight_smile:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms