Hello, i need the player to face the camera’s direction but the script i made makes the player spin infintely
RunService:BindToRenderStep("CharacterDirection",Enum.RenderPriority.First.Value,function()
local cameraCFrame = camera.CFrame
local humanoidRootPartCFrame: CFrame = humanoidRootPart.CFrame
--Transform the look vector of the humanoid root part to
--the camera 3D space coordinate by multiplying the camera's
--CFrame with the look vector
local humanoidRootPartRelativeToCameraCFrameXZ = cameraCFrame * ((humanoidRootPartCFrame.LookVector * Vector3.new(1,0,1)).Unit)
--Tries to calculate the offset angle between the lookvector
--And the horizontal of the camera's 3D space (X axis of the camera's 3D space)
local angleOfRotation = math.atan2(humanoidRootPartRelativeToCameraCFrameXZ.Z,humanoidRootPartRelativeToCameraCFrameXZ.X)
humanoidRootPart.CFrame = humanoidRootPart.CFrame * CFrame.Angles(0,angleOfRotation,0)
end)
Any help would be appreciated, and please provide an explanation if you have a somewhat long/math/hard solution
The issue you’re experiencing with the player spinning infinitely is likely due to the continuous update of the angleOfRotation
without any condition to stop or stabilize it.
-- Assuming you have these defined somewhere in your script
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = game.Workspace.CurrentCamera
local humanoid = script.Parent.Humanoid
local humanoidRootPart = script.Parent.HumanoidRootPart
local function updateCharacterDirection()
local cameraCFrame = camera.CFrame
local humanoidRootPartCFrame = humanoidRootPart.CFrame
-- Calculate the look direction vector of the humanoid relative to the camera
local lookVector = cameraCFrame.Position - humanoidRootPartCFrame.Position
lookVector = Vector3.new(lookVector.X, 0, lookVector.Z).Unit -- Ignore vertical component
-- Calculate the rotation to face the camera
local desiredRotation = CFrame.new(humanoidRootPart.Position, humanoidRootPart.Position + lookVector)
-- Smoothly rotate the character towards the desired rotation
humanoidRootPart.CFrame = desiredRotation
end
-- Bind to RenderStepped for continuous update
RunService.RenderStepped:Connect(function()
updateCharacterDirection()
end)
Key things to take out of this code block:
-
Vector Calculation:
lookVector
calculates the direction from the player’s root part (humanoidRootPart
) to the camera, ignoring the vertical component (Y-axis) to avoid unwanted rotations.
-
Desired Rotation:
desiredRotation
constructs a CFrame
that rotates the humanoidRootPart
to face the calculated lookVector
.
Make sure to replace script.Parent
with the actual reference to your player’s model. This script assumes it’s placed within the player’s character.
2 Likes
Hello, sorry for the late response. I have tested your code but now the player is facing the camera and not the camera’s direction. I have tried to switch to the opposite of the lookvector relative to the camera but now the player doesn’t face the camera perfectly.
Example of what i mean :
When i change the look vector to its opposite (Not looking perfectly at the camera direction like shift lock would do) :
With shift lock :
I would also preferebly have a solution using trigonometry because i’m bad with it and i wish to improve