Rotate on X axies towards an objects height in 3D

Pretty much what I need to do is simulate an AI looking up and down in the same way that players do (with a separate object). Something like the AIs head looks up and down at the player but doesn’t turn because his whole body turns instead. I’m having trouble with this and all my searches have come up with stuff that isn’t quite what I need.

I managed to make some code do exactly what I want (including not rotating past 90/-90) but only if the player is standing at a specific side of the AI.

		RotationVar = Quaternion.LookRotation(transform.position - Target.position, Vector3.right);
		RotationVar.y = 0;
		RotationVar.z = 0;
		RotationVar.eulerAngles = transform.TransformDirection(new Vector3 (-RotationVar.eulerAngles.x, 0, 0));
		transform.rotation = Quaternion.Slerp(transform.rotation, RotationVar, VerticalTurnSpeed * Time.deltaTime);

The transform.TransformDirection doesn’t seem to have any effect on this as it still only works correctly in 1 direction (if the player is standing in a different direction it still faces that same direction as if it was doing it in global.

Any help would be appreciated.

I’m having to guess a bit about what you want to happen. I’m guessing you want the body to rotate around without looking up and down, and you want the head to do the up/down looking. This kind of movement is the same as used for Turrets…like a tank with a turret that goes around and a gun that handles elevation. If so, there are a number of different posts concerning this behavior. If the body is always aligned with world ‘up’, here is one post that should work: