Body not rotating?

Hey, im using this script to attach the camera to the head, but i have one problem, when i move the camera around, the body does not rotate with the camera (as it would do on first person or shiftlock) the CameraMode is set to LockFirstPerson and it still doesnt work.

Heres the code!

local RunService = game:GetService("RunService")
local InputService = game:GetService("UserInputService")

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.AutoRotate = false

local CurrentCamera = workspace.Camera
CurrentCamera.CameraType = Enum.CameraType.Scriptable

local Head = Character:WaitForChild("Head")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local CameraRotation = Vector2.new(0, 0)
local RotationSpeed = Vector2.new(1, 1)
local BodyRotationSpeed = 10
local SmoothnessFactor = 0.1

for _, part in ipairs(Character:GetChildren()) do
	if part:IsA("BasePart") and not (part.Name:match("Arm") or part.Name:match("Torso") or part.Name:match("Leg")) then
		part.Transparency = 1
		part.CanCollide = false
	elseif part:IsA("Accessory") then
		part:Destroy()
	end
end

InputService.InputChanged:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseMovement then
		CameraRotation += Vector2.new(Input.Delta.X * -1, Input.Delta.Y * -1) * RotationSpeed
		CameraRotation = Vector2.new(CameraRotation.X, math.clamp(CameraRotation.Y, -80, 80))
	end
end)

RunService.RenderStepped:Connect(function()
	InputService.MouseBehavior = Enum.MouseBehavior.LockCenter

	local targetCameraRotation = Vector2.new(CameraRotation.X, CameraRotation.Y)
	CameraRotation = CameraRotation:Lerp(targetCameraRotation, SmoothnessFactor)

	local HorizontalAngle = math.rad(CameraRotation.X)
	local VerticalAngle = math.rad(CameraRotation.Y)

	CurrentCamera.CFrame = Head.CFrame * CFrame.fromEulerAnglesYXZ(VerticalAngle, HorizontalAngle, 0)

	if HumanoidRootPart then
		local direction = Vector3.new(math.sin(HorizontalAngle), 0, math.cos(HorizontalAngle))
		HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, HumanoidRootPart.Position + direction)
	end
end)

Thanks!

1 Like

You’re using the head to “localize” the camera CFrame. But then you want to move the HRP with respect to the camera rotation. The HRP then rotates the head, resulting in a kind of infinite rotation when I tried to set the HRP cframe to the camera cframe look vector.

Here’s what I changed that worked:

RunService.RenderStepped:Connect(function()
	InputService.MouseBehavior = Enum.MouseBehavior.LockCenter

	local targetCameraRotation = Vector2.new(CameraRotation.X, CameraRotation.Y)
	CameraRotation = CameraRotation:Lerp(targetCameraRotation, SmoothnessFactor)

	local HorizontalAngle = math.rad(CameraRotation.X)
	local VerticalAngle = math.rad(CameraRotation.Y)
	
	local new_camera_cf = CFrame.fromEulerAnglesYXZ(VerticalAngle, HorizontalAngle, 0) + Head.CFrame.Position
	
	CurrentCamera.CFrame = new_camera_cf

	if HumanoidRootPart then
		local direction = Vector3.new(new_camera_cf.LookVector.X, 0, new_camera_cf.LookVector.Z)
		HumanoidRootPart.CFrame = CFrame.lookAlong(HumanoidRootPart.Position, direction, Vector3.yAxis)
	end
end)
1 Like

Thanks! It kinda works, the problem is that now the camera isnt attaching to the head at all.

What do you mean with “attaching”? is the camera somewhere else, on the ground, in the air? provide a video if possible, thanks.

so you want the camera and head to be the same cframe?
Then you’re getting into trying to take over the Neck Motor6D inside of Head.
The animator generally controls that.

Heres a video of what im trying to get:

(dont mind the bugs on the camera)
The issue with this actual script is that the body doesnt really rotate at all.

What im trying to do, is the camera be in the same cframe as the head, but with camera movement freedom.
(as normal camera)

If you create a blank localscript called “Animate” inside of StarterCharacterScripts it will remove all default animations. Then just set StarterPlayer.CameraMode to LockFirstPerson. No programming required.
image

I already did that and still didnt work, whats the point on doing it anyways?

I see what you were saying now. Sorry for my short response.
Try this

RunService.RenderStepped:Connect(function()
	InputService.MouseBehavior = Enum.MouseBehavior.LockCenter

	local targetCameraRotation = Vector2.new(CameraRotation.X, CameraRotation.Y)
	CameraRotation = CameraRotation:Lerp(targetCameraRotation, SmoothnessFactor)

	local HorizontalAngle = math.rad(CameraRotation.X)
	local VerticalAngle = math.rad(CameraRotation.Y)

	CurrentCamera.CFrame = Head.CFrame * CFrame.fromEulerAnglesYXZ(VerticalAngle, 0, 0)

	if HumanoidRootPart then
		local direction = Vector3.new(math.cos(HorizontalAngle), 0, -math.sin(HorizontalAngle))
		HumanoidRootPart.CFrame = CFrame.lookAlong(HumanoidRootPart.Position, direction)
	end
end)

Basically I removed the Horizontal angle from the Camera rotation, since this is “handled” in the rotation of the HRP.

Lastly, put

CurrentCamera.CFrame = Head.CFrame * CFrame.fromEulerAnglesYXZ(VerticalAngle, 0, 0)

after the HRP rotation. This seems to make it much smoother.