How to make the player keep facing the part while moving?

I’m using bodygyro to let the player face a certain part and it works, but I want the player to KEEP facing the part during a period of time while hes moving. How do I achieve this? Right now hes sidewalking :confused:.

Player.Character.Humanoid.AutoRotate = false
	wait(0.1)
	local BodyGyro = Instance.new("BodyGyro")
	BodyGyro.CFrame = CFrame.new(Player.Character.PrimaryPart.Position, Target.PrimaryPart.Position)
	BodyGyro.MaxTorque = Vector3.new(0, 400000, 0)
	BodyGyro.P = 400000
   	BodyGyro.D = 100
	BodyGyro.Parent = Player.Character.PrimaryPart
5 Likes

So you want to have it face the object without having the player have to move their controls.

What you can do is edit the C0 of the humanoidrootpart to lower torso to make it look like the player is facing a different way but letting the player keep the same controls.
You should keep autorotate on the and set the c0 to face the part.

Edit: This next part should be in a loop

local x,y,z = CFrame.new(rootpartpos, otherpartpos):ToEulerAnglesXYZ()
joint.C0 = CFrame.new(joint.C0.Position) * CFrame.Angles(x,y,z)

On mobile so apologies if theyres any mistakes!

Honestly, I would avoid messing with the C0 Joint as what you’re essentially doing is offsetting the Torso from the HumanoidRootPart.

This can cause certain animations to be altered, or can mess with any potenial procedural animation as well.

Your math is spot on, but I would recommend either throwing it a heartbeat or a render stepped loop depending on how accurate frame-by-frame you want the character aligned.

As long as the Move call is using the relative parameter to the camera, or you’re simply using vanilla camera scripts, the character should continue wherever the camera is looking.

Such a loop may look like:

game:GetService('RunService').RenderStepped:connect(function()
      Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(0,CFrame.new(Character.HumanoidRootPart.Position, TargetPart.Position):ToEulerAngleXYZ().Y,0)
end)

Best of luck!

This would end up making the player have to change their keys though.

Couldn’t you do a runservice on the bodyGyro itself?

The player won’t need to change keys because the move command that the vanilla scripts call on the character use the relative parameter by default.

Here is the correct and tested version:

game:GetService('RunService').RenderStepped:connect(function()
      Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(0,Vector3.new(CFrame.new(Character.HumanoidRootPart.Position, TargetPart.Position):ToOrientation()).Y,0)
end)

This is simpler and more reliable than dealing with physics.

Hope this helps.

https://gyazo.com/f1d1473547f7bfc14791253561991271

7 Likes

Oh okay, so you’re saying that bodyGyro also uses physics calculation that is avoided by using your method and that would save calculations.

A body gyro is a BodyMover (a physics instance), it uses the physics clock to manipulate the properties of an instance.

For your use-case simply setting the CFrame of your character is way simpler and more reliable then dealing with physics which can be impacted upon by other external forces.

2 Likes

Exactly what I was looking for this morning when I decided I want my NPCs to always be looking at the player.

1 Like