How to make rotation with quaternions along one axis? So that the character does not lean.

Hello everyone.
Here is my script.

void povorotNaVraga()
        {
            Vector3 relativePos = Personag_vrag.teloPersonaga.transform.position - transform.position;
            rot = Quaternion.LookRotation(relativePos);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * 400);
            if (Mathf.Abs(Quaternion.Dot(transform.rotation, rot)) <0.99f)
            {
                povorotBool = false;
            }
            else
            {
                transform.rotation = rot;
                povorotBool = true;
            }
        }

What needs to be fixed in my function? Or maybe a better way?

Well, the issue is probably your “relativePos”. If the two objects are not exactly at the same height, you get a vector that is tilted up or down. So you probably want to get rid of any “y” offset. You can simply do:

Vector3 relativePos = Personag_vrag.teloPersonaga.transform.position - transform.position;
relativePos.y = 0f;
rot = Quaternion.LookRotation(relativePos);
// [ ... ]
1 Like

Thank you. This is what I need.