What's a more efficient way of rotating a R15 to face a player?

Currently trying to make a boss fight in which the boss tracks onto the player and rotates to follow/watch them. I’m currently just using CFrame to do this as I maintain the current position of the boss but change the rotation to the players. This obviously works for X and Z but for Y the boss just falls over. I’m looking for a more efficient way of doing this. One that involves the upper body rotating up to look at the player while the body stays grounded.

Here’s my code so far:

repeat task.wait()
	if tween == nil and player then
		local modelPos = enemy.PrimaryPart.Position
		local playerPos = player.Character.PrimaryPart.Position
		local lookVector = (playerPos - modelPos).unit
		local lookRotation = CFrame.new(modelPos, modelPos + lookVector)
		local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
		tween = game:GetService("TweenService"):Create(enemy.PrimaryPart, tweenInfo, {CFrame = lookRotation})
	enemy:SetPrimaryPartCFrame(CFrame.new(enemy.HumanoidRootPart.Position, enemy.PrimaryPart.Position + (player.Character.HumanoidRootPart.Position - enemy.PrimaryPart.Position).Unit))
until not attackAny.IsPlaying or not player

One easy way would be to just use the CFrame.new(originPosition,faceTowardsPosition) constructor, and make the faceTowardsPosition's Y value equal the same as the originPosition's Y value.

1 Like

A very easy way is using CFrame.lookAt()

I changed the look rotation to be:

local lookRotation = CFrame.lookAt(modelPos,Vector3.new(player.Character.HumanoidRootPart.Position.X, 0, player.Character.HumanoidRootPart.Position.Z))

but the enemy still faces upwards

The Y axis needs to equal that of the Y position of modelPos. Although unless your model is below the Y=0 Axis, I’m not sure why it would be facing upwards rather then down, unless your model orientation is incorrect.

It was an error in my code. Now I need to work on rotating Motor6Ds to face upwards/downwards

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.