How to make a script that rotates the player in whatever direction they're moving?

I’m trying to make a script right now that makes the player face in whichever direction their character model is being moved. Here’s the problems I’m having:

  • Sometimes getting flung into the void
  • Character rotation keeps resetting to be upright even when the player is moving down.
  • Just a lot of other weird issues. Just see the video haha

Here’s a video:

Here’s my code for testing this:

while task.wait() do
	local HumanoidRootPart = game:GetService("Players").LocalPlayer.Character:WaitForChild("HumanoidRootPart")
	HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, HumanoidRootPart.Position + HumanoidRootPart.Velocity)
end

If you have any suggestions for fixing this, it would be greatly appreciated.

1 Like

So, theres a couple of spottable problems:

Which direction is this?

CFrame.lookAt(HumanoidRootPart.Position, HumanoidRootPart.Position + Vector3.new(0,0,0))

When the Cframe constructor cant define the direction it goes crazy and ur char dies.

Sinse your humanoid might be in Freefall state, it automatically tries to set the character to a straight position. To avoid that you can set the Humanoid’s state to Physics which is a safe state for you to calculate its movement.

Humanoid:ChangeState(Enum.HumanoidStateType.Physics)

Velocity property is deprecated, try HumanoidRootPart.AssemblyLinearVelocity.

A better approach for moving your character like this is the use of constraint movers like AlignOrientation + LinearVelocity

Also see this

The player actually already has a LinearVelocity. It’s parented to the HumanoidRootPart and it’s named “PrimaryVelocity”. I’m not familiar with AlignOrientation, though. Could you elaborate?

Also, excellent response. Very well explained.

AlignOrientation works just like the old BodyGyro. Instead of making the object move, it makes the object turn to a desired direction, you would want something like:

-- Create but leave disabled:
local AlignOrientation = Instance.new("AlignOrientation")
AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
AlignOrientation.Attachment0 = Root.RootAttachment -- attachment in the root part
AlignOrientation.RigidityEnabled = true
AlignOrientation.Enabled = false
AlignOrientation.Parent = Root


-- then when you enter a state you are going to use it, enable:
AlignOrientation.Enabled = true

-- Setting desired cframe:
local DirectionChangeConnection
DirectionChangeConnection = game:GetService("RunService").RenderStepped:Connect(function()
    if Root.AssemblyLinearVelocity ~= Vector3.zero then
        AlignOrientation.CFrame = CFrame.lookAlong(Root.Position, Root.AssemblyLinearVelocity)
    else
        AlignOrientation.CFrame = CFrame.lookAlong(Root.Position, Root.CFrame.LookVector)
    end
end)

-- when disabling that state:
AlignOrientation.Enabled = false
if DirectionChangeConnection then 
    DirectionChangeConnection:Disconnect() 
    DirectionChangeConnection = nil 
end
1 Like

Aren’t you the TSB youtuber? I thought you quit developing.
Anyway, It looks like a humanoid state problem with the upright thing. For the velocity, use the normalized vector (.Unit) of the Humanoid’s MoveDirection and it SHOULD work. Do math with the camera angle to make the character face the camera (ish)

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