makes the head of the player follow where the camera is looking at.
In StarterPlayer > StarterPlayerScripts, add a LocalScript.
Rename the script if you like (e.g., "HeadFollowCamera").
Here's the code for the script:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local runService = game:GetService("RunService")
-- Wait for the character to load
character:WaitForChild("Head")
-- Run a function every frame to update head rotation
runService.RenderStepped:Connect(function()
if character and character:FindFirstChild("Head") then
local head = character.Head
local cameraDirection = camera.CFrame.LookVector -- Get camera look direction
local bodyYaw = Vector3.new(cameraDirection.X, 0, cameraDirection.Z).Unit -- Horizontal direction only
-- Get the desired rotation for the head
local lookAtAngle = CFrame.lookAt(head.Position, head.Position + bodyYaw)
-- Smoothly update the head's rotation to match the camera's direction
head.CFrame = CFrame.new(head.Position) * CFrame.Angles(0, lookAtAngle.Y, 0)
end
end)
RenderStepped: This function runs every frame on the client, making it ideal for smooth, real-time updates.
camera.CFrame.LookVector: Retrieves the direction in which the camera is pointing.
CFrame.lookAt: Computes the CFrame that would make the head face toward the calculated position.
head.CFrame: Adjusts the head’s rotation to align with the camera’s direction without affecting its position.
This script only works locally, so it won’t replicate to other players. This is ideal if you only want the player to see their own head moving.
If you want it to replicate to others, use a RemoteEvent to communicate the camera direction to the server.
the players character will tilt in the direction of left or right.
In StarterPlayer > StarterPlayerScripts, add a LocalScript.
Rename it to something descriptive, like "PlayerTiltScript."
Here’s the code for the script:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local runService = game:GetService("RunService")
-- Parameters for tilt
local tiltAmount = math.rad(15) -- How much the player tilts, in radians (15 degrees here)
local tiltSpeed = 0.2 -- Smoothness of the tilt effect
runService.RenderStepped:Connect(function()
local moveDirection = humanoid.MoveDirection
-- Determine tilt based on direction
local targetTilt = 0
if moveDirection.X < 0 then
targetTilt = tiltAmount -- Tilt left
elseif moveDirection.X > 0 then
targetTilt = -tiltAmount -- Tilt right
end
-- Smoothly interpolate towards the target tilt angle
rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, 0, (targetTilt - rootPart.CFrame:ToObjectSpace(rootPart.CFrame).Z) * tiltSpeed)
end)