Little rotation help

I have a model of a man, that looks in players direction, when close. But when the player jumps, he rotates in a weird upwards rotation, where he almost "lies down". How to prevent him from rotating like that?

This is a piece of my script.

rotate = Quaternion.LookRotation(player.position - transform.position); // 
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * slower);

1 Answer

1

It sounds like you want the model to track the player while only rotating about the 'up' axis. To achieve this, replace this:

rotate = Quaternion.LookRotation(player.position - transform.position);

With this (untested):

Vector3 difference = player.position - transform.position;
difference.y = 0; // Syntax here may be language-dependent
rotate = Quaternion.LookRotation(difference);

Sorry, the syntax for the first line will probably be language-dependent as well. (In other words, you'll need to adapt what I posted based on which scripting language you're using.)

Have you tried it? (rotate.y = 0, that is...)

You can try it (and it may work), but note that manipulating the elements of a rotation quaternion directly may not always have the effect you expect. Personally, I'd go with the method I posted (perhaps with a length check to catch the case where the player is more or less directly above or below the agent). But, YMMV.