Character rotating on supposedly locked axis

Right now I am trying to get my character to face a targeted enemy. Everything works great except for the fact my character will face the targeted enemy on every axis, not just the Y axis.

Here is an example of the bug.

I dont want the character to rotate to face the enemy downward, because of the other glitch i showed in the video where the player can walk backwards into the air. Here is the code I’m using to have the player look at the enemy.

public void lookAtTarget(){
	if (target != null) {
		Vector3 lookDirection = new Vector3 (Player.position.x, target.position.y, Player.position.z);
		rig.transform.rotation = Quaternion.Lerp (rig.transform.rotation, Quaternion.LookRotation (lookDirection.normalized), Time.deltaTime * 12f);
		}
	}

Ive also tried variations of the lookDirection vector, including

Vector3 lookDirection = new Vector3 (0.0F, target.position.y, 0.0F);

and

Vector3 lookDirection = new Vector3 (target.position.x, 0.0F, target.position.z);

if anyone has any ideas on how to fix this, help would be very much appreciated. I cant seem to get it to work at the moment, no matter what else I try.

The Y direction is the “Up and Down” direction. You are only rotating the player to face the object as it goes up and down.

I’m not sure why you are getting X and Z targeting but maybe if I saw more code it would become clear.

Anyway if the target’s y value goes below the player’s y value just stop following it in the Y direction.

Vector3 lookDirection = target.position - player.position; // this vector points from player to target

if ( lookDirection.y < 0)
{
    lookDirection.y = 0;
}

Try this:

   Vector3 lookPos = target.position - transform.position;
    lookPos.y = 0;
    
    Quaternion rotation = Quaternion.LookRotation(lookPos);
    
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);