Hi, I am attempting to create a First Person movement camera script which allows the player to move in one direction and also look around with their camera freely. The problem is that I want to make it so that the player cannot look behind their characters back. I have already achieved this to some level by clamping the xAngle variable to -90 or 90 around the front of the character torso.
local userInputService = game:GetService("UserInputService") local runService = game:GetService("RunService") local localPlayer = game:GetService("Players").LocalPlayer local Camera = game:GetService("Workspace").CurrentCamera local xAngle = 0 local yAngle = 0 local cameraPos = Vector3.new(0,0,0) wait(1) Camera.CameraType = Enum.CameraType.Scriptable userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter local Character = localPlayer.Character local HRP = Character:WaitForChild("HumanoidRootPart") userInputService.InputChanged:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseMovement then xAngle = xAngle-input.Delta.x*0.4 yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80) end end) runService.RenderStepped:Connect(function() local Character = localPlayer.Character local rootPart = Character:FindFirstChild("HumanoidRootPart") if Character and rootPart then local xa = HRP.Orientation.y - 90 local xb = HRP.Orientation.y + 90 xAngle = math.clamp(xAngle, xa, xb) local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0) local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z)) local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000)) Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p) end end)
It looks good to begin with, you cant look behind your character which is exactly what I wanted. The problem comes when you attempt to rotate your character around your camera. For some reason when you do this and the camera will start to freak out and glitch a lot. I am not sure what is causing this or how to stop this from happening. Any help would be appreciated greatly.