How to Rotate a CFrame on only the X and Z

I have a mob chasing a player and part of that chase code has the mob turn toward the player, like so:

mobPart.CFrame = CFrame.new(mobPart.Position, targetPart.Position)

The issue I have is, if the player is jumping then the mob will rotate upwards in the Y axis and I don’t want it to do that. These mobs will ALWAYS stay on the same Y axis and I want their X and Z axes to always stay perpendicular to the Y.

Is this what are you looking for?

local desiredYaxisPosition = 32 -- Change it to the desired y axis value
mobPart.CFrame = CFrame.new(
	mobPart.Position,
	Vector3.new(
		targetPart.Position.X,
		desiredYaxisPosition,
		targetPart.Position.Z
	)
)
1 Like