I'm trying to lock camera CFrame to head, but player can rotate camera. And i see bugs when player go sideways/backward

local runservice = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local hum = char:FindFirstChildOfClass("Humanoid")
local cam = workspace.CurrentCamera
local head = char:WaitForChild("Head")
local ts = game:GetService("TweenService")
local input = game:GetService("UserInputService")
Sensitivity = 0.6	
Smoothness = 0.05
local camcframe = cam.CFrame
HeadOffset = CFrame.new(0,0,-0.3)
local humanoidpart = char.HumanoidRootPart

local screenSize = cam.ViewportSize
local screenCenter = (screenSize * 0.5)
local angle 
local walkvector = Vector3.new(0,0,0)

input.InputChanged:Connect(function(inp,gamepr)
	if inp.UserInputType == Enum.UserInputType.MouseMovement then
		
		angle = Vector2.new(mouse.X,mouse.Y) - screenCenter
		
	end
end)

--local controls = require(game:GetService("Players").LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
--controls:Disable()
hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if math.round(hum.MoveDirection.X) == -1 then
		print("Walking left!")
		
	
	elseif math.round(hum.MoveDirection.X) == 1 then
		print("Walking right!")
		
	elseif math.round(hum.MoveDirection.Z) == -1 then
		print("Walking forward!")
		
	elseif math.round(hum.MoveDirection.Z) == 1 then
		print("Walking backward!")
	
	end
end)

runservice.RenderStepped:Connect(function()
	cam.CameraType = Enum.CameraType.Scriptable
	local result =  head.CFrame * CFrame.Angles(0,math.rad(-angle.X/11.25),0) * CFrame.Angles(math.rad(-angle.Y/5.625),0,0) * HeadOffset
	cam.CFrame = cam.CFrame:Lerp(result,1)
	

humanoidpart.CFrame = CFrame.new(humanoidpart.Position) * CFrame.Angles(0,math.rad(-angle.X/5.625),0)
	
end)
1 Like

Can you explain a bit more clearly on what these “bugs” are?

Also instead of printing if they walked left or right you could save the cframe of the heads position and when they walk right it sets the head cframe to the stored cframe that we had gotten earlier.

^^ This is suggested because of your lack of disclosure on the “bugs”

Since the CFrame camera is attached to the head, it also shakes, like the head, but when the player turns the camera to the side (for example, to the left and let’s say he holds A and so he goes sideways), the camera starts to shake strongly, and this also happens with a camera if the player walks backwards

Have you tried ignoring the roation on the cframe? That seems to be what is going on.

image

What I mean is to pretty much set the orientation to 0,0,0 although I recommend to figure out what is the root cause you could sit on the properties tab and monitor the CFrames as you move around and observe the behaviour.

1 Like

yay! it works! but my camera after i stopping walking is not smoothly moves. How to fix it?

 local runservice = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local hum = char:FindFirstChildOfClass("Humanoid")
local cam = workspace.CurrentCamera
local head = char:WaitForChild("Head")
local ts = game:GetService("TweenService")
local input = game:GetService("UserInputService")
Sensitivity = 0.6	
Smoothness = 0.05
local camcframe = cam.CFrame
HeadOffset = CFrame.new(0,0,-0.3)
local humanoidpart = char.HumanoidRootPart

local screenSize = cam.ViewportSize
local screenCenter = (screenSize * 0.5)
local angle 
local walkvector = Vector3.new(0,0,0)

input.InputChanged:Connect(function(inp,gamepr)
	if inp.UserInputType == Enum.UserInputType.MouseMovement then
		
		angle = Vector2.new(mouse.X,mouse.Y) - screenCenter
		
	end
end)

--local controls = require(game:GetService("Players").LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
--controls:Disable()

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	local Xcam, Ycam, Zcam = cam.CFrame:ToEulerAnglesXYZ() 

	if math.round(hum.MoveDirection.X) == -1 then
		print("Walking left!")

		humanoidpart.CFrame = humanoidpart.CFrame:Lerp(CFrame.new(humanoidpart.Position) * CFrame.Angles(0,math.rad(-angle.X/22.5),0),1)
		
	
	elseif math.round(hum.MoveDirection.X) == 1 then
		print("Walking right!")
		humanoidpart.CFrame = humanoidpart.CFrame:Lerp(CFrame.new(humanoidpart.Position) * CFrame.Angles(0,math.rad(-angle.X/22.5),0),1)
		
	elseif math.round(hum.MoveDirection.Z) == -1 then
		print("Walking forward!")
		humanoidpart.CFrame = humanoidpart.CFrame:Lerp(CFrame.new(humanoidpart.Position) * CFrame.Angles(0,0,0),1)
	elseif math.round(hum.MoveDirection.Z) == 1 then
		print("Walking backward!")
		humanoidpart.CFrame = humanoidpart.CFrame:Lerp(CFrame.new(humanoidpart.Position) * CFrame.Angles(0,0,0),1)
	end
	if hum.MoveDirection == Vector3.new(0,0,0) then
		humanoidpart.CFrame = humanoidpart.CFrame:Lerp(CFrame.new(humanoidpart.Position) * CFrame.Angles(0,0,0),1)

	end
end)




runservice.RenderStepped:Connect(function()
	cam.CameraType = Enum.CameraType.Scriptable
	local result =  head.CFrame * CFrame.Angles(0,math.rad(-angle.X/22.5),0) * CFrame.Angles(math.rad(-angle.Y/5.625),0,0) * HeadOffset
	cam.CFrame = cam.CFrame:Lerp(result,1)
	
	humanoidpart.CFrame = CFrame.new(humanoidpart.Position) * CFrame.Angles(0,math.rad(-angle.X/5.625),0) 
	
end)

robloxapp-20240507-1754247.wmv (4.6 MB)
here i test

robloxapp-20240507-1757168.wmv (1.3 MB)
Here i just going sideways, and camera is not smoothly moving