Im trying to make a stationary camera but I’m struggling with making it look around and the result I get from:
local runservice = game:GetService("RunService")
local tweenservice = game:GetService("TweenService")
local userinpuservice = game:GetService("UserInputService")
userinpuservice.MouseBehavior = Enum.MouseBehavior.LockCenter
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local setup = game.Workspace["Player Setup"]
local camerapart = setup:WaitForChild("Camera")
camera.CameraType = Enum.CameraType.Scriptable
userinpuservice.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local x,y,z = camerapart.CFrame:ToEulerAnglesXYZ()
local angle = CFrame.Angles(math.rad(x+input.Delta.X),math.rad(y+input.Delta.Y),math.rad(z+input.Delta.Z))
local rotate = tweenservice:Create(camerapart,TweenInfo.new(0.05),{["CFrame"] = camerapart.CFrame * angle})
rotate:Play()
end
end)
runservice.RenderStepped:Connect(function()
camera.CFrame = camerapart.CFrame
end)
Results in this:
does anyone know how do do this properly or fix this?
Code is a simplified version of Create a Custom CameraScript from documentation.
local camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
camera.CameraType = Enum.CameraType.Scriptable
local cameraRotation = Vector2.new(0,math.rad(0))
RunService.RenderStepped:Connect(function()
if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
else
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
camera.CameraType = Enum.CameraType.Scriptable
--keep current rotation move to humanoid root part
local headPosition = player.Character.Head.Position
local mouseDelta = UserInputService:GetMouseDelta()
cameraRotation = cameraRotation - mouseDelta/200
local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0)*CFrame.Angles(cameraRotation.Y, 0, 0)
camera.CFrame = cameraRotationCFrame+headPosition -- rotate
end)