CFrame looking at an item, fix

To be more specific, I have a combat script where the player looks at an enemy. I use;

character:SetPrimaryPartCFrame(CFrame.new(character.HumanoidRootPart.Position, enemy.HumanoidRootPart.Position))

To look at the enemy.

This works but it gets glitchy when the enemy is higher, or lower than the player. The player then starts walking on air or going down to fix its CFrame. I was wondering if there’s an alternate to just look where the enemy is without this buggy mess?

This is pretty vague, but if you don’t want the resulting CFrame to be rotated on the local X axis (looking up and down) then use the character’s own Y position in the Position to look at. So that only the enemy’s X and Z are used.

For anyone looking back at this, solution was:

character:SetPrimaryPartCFrame(CFrame.lookAt(character.HumanoidRootPart.Position, Vector3.new(enemy.HumanoidRootPart.Position.X, character.PrimaryPart.Position.Y, enemy.HumanoidRootPart.Position.Z)))
1 Like