Camera look at players Y position only

Hi there, I’m making a 3D platformer and I’m having issues creating a dynamic camera script which allows for a camera to follow the players position in a variety of ways.

Basically when the horizontal and vertical rotation are disabled it does nothing which is correct. When both are enabled the camera will always look at the player even if they jump. When vertical rotation is disabled the camera won’t rotate up and down with the players jump which is correct.

My problem is that in the scenario when horizontal rotation is disabled the cameras will just start rotating to 90 degrees on the X axis when the player’s Y position stays the same but it will suddenly rotate up drastically when they jump. I’ve included a snippet of my script below and really need some help figuring out my problem. If anyone has any ideas why the cameras constantly rotate by themselves I’d greatly appreciate the feedback.

public sealed class CameraLookAtPlayer : MonoBehaviour 
{
	// Unity modifable values
	[SerializeField, Range (0.1f, 10f)] private float m_speed = 1f;	// How fast the camera will rotate
	[SerializeField, Range (0f, 5f)] private float m_yOffset = 1f;	// Used in stopping the camera looking at the characters feet.
	[SerializeField] private bool m_horizontalRotation = true;		// Should the camera rotate horizontally or not
	[SerializeField] private bool m_verticalRotation = true;		// Should the camera rotate vertically or not

	// Member variables
	private Transform m_player;	// The players current transform

    
	private void LookAtPlayer()
	{
		// Calculate desired direction
		Vector3 direction = m_player.position - transform.position;

		// Add the chosen offset to effect the height of the rotation
		direction.y += m_yOffset;

		// Disable horizontal rotation
		if (!m_horizontalRotation)
		{
			direction.x = transform.forward.x;
			direction.z = transform.forward.z;
		}

		// Disable vertical rotation
		if (!m_verticalRotation)
		{
			direction.y = transform.forward.y;
		}

		if (direction != Vector3.zero)
		{
			// Slerp the direction to create fluid movement
			Quaternion lookAt = Quaternion.LookRotation (direction);
			transform.rotation = Quaternion.Slerp (transform.rotation, lookAt, m_speed * Time.deltaTime);
		}
	}
}

Turns out it was just an issue with the direction not being normalised which only occurred when rotating vertically on it’s own. The fix was:

		// Calculate desired direction
		Vector3 direction = m_target.position - transform.position;

		// Add the chosen offset to effect the height of the rotation
		direction.y += m_offsetY;

		// Correct magnitude
		if (direction.magnitude > 1)
		{
			direction.Normalize();
		}