So my game is in first person but I want the players top half bend to where the camera is looking. There may be a tutorial out there, but so far I’ve found nothing. There’s a tutorial for turning the head but instead I want to rotate the body.
5 Likes
The word you use here, “bend,” is confusing to me. You just want the entire character to face where the camera is looking? Or just the top half?
1 Like
top half of the character, let me change that
1 Like
So you mean if the camera is facing up the top half bends upward and if the camera faces the ground the top half leans down?
1 Like
yes, I rephrased it, sorry about that
1 Like
I made this script to do that a while ago. Put it in StarterCharacterScripts to test it. Here is a video of what it does.
-- StarterCharacter script
local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local char = player.Character
local human = char:WaitForChild("Humanoid")
local rootPart = char.PrimaryPart
local upperTorso = char:WaitForChild("UpperTorso")
local waistJoint = upperTorso:WaitForChild("Waist")
-- Services
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
-- Globals
local currentSpeed = 0;
local currentTween = nil;
human.Running:Connect(function(speed)
currentSpeed = speed
end)
-- This was actually not needed to be disabled
--human.AutoRotate = false
local originalWaistC1 = waistJoint.C1
local maxRotation = math.rad(65)
runService:BindToRenderStep("vanityCamera", 400, function()
local targetPosition = (cam.CFrame * CFrame.new(0,0,-1000)).p
local torsoFront = rootPart.CFrame.LookVector
local torsoRight = rootPart.CFrame.RightVector
local vectorToTarget = (targetPosition - rootPart.Position)
local rotation = math.atan2(torsoRight:Dot(vectorToTarget), torsoFront:Dot(vectorToTarget))
-- Clamp the rotation
if (rotation < -maxRotation) then
rotation = -maxRotation
elseif (rotation > maxRotation) then
rotation = maxRotation
end
if (math.abs(rotation) == maxRotation and currentSpeed <= 0.5) then
-- Rotate the bottom half to face the new direction
local newRootPartCFrame = CFrame.new(rootPart.Position, Vector3.new(
targetPosition.X, rootPart.Position.Y, targetPosition.Z
))
currentTween = tweenService:Create(rootPart, TweenInfo.new(0.33), {
CFrame = newRootPartCFrame
})
currentTween:Play()
else
if (currentTween and currentSpeed > 0.5) then
if (currentTween.PlaybackState == Enum.PlaybackState.Playing) then
currentTween:Cancel()
end
end
waistJoint.C1 = CFrame.Angles(0,rotation,0) * originalWaistC1
end
end)
16 Likes