I am trying to create a toggleable over the shoulder third person camera. I have the whole thing working, besides for a glitch where the OTS camera always points to the positive Z direction, no matter which way your character is facing. I want that when it toggles on, it turns the camera to be facing the same direction as the character, not always face Positive Z. In the place where I set the camera CFrame, I tried setting the lookat to the character direction, but that didn’t work. I couldn’t find anything on the DevForum that could help me.
This is the script I currently have. It is a local script in the gun tool.
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local event
local camera = workspace.CurrentCamera
local cameraOffset = Vector3.new(2, 2, 8)
local player = Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Equipped:Connect(function()
mouse.Icon = "rbxassetid://9524023207"
local charCFrame = player.Character.HumanoidRootPart.CFrame
local charAngle = charCFrame - charCFrame.Position
charCFrame = charCFrame * CFrame.Angles(0, 0, 0)
charCFrame = charAngle
local function setCam()
local humanoid = player.Character:WaitForChild("Humanoid")
local rootPart = player.Character:WaitForChild("HumanoidRootPart")
humanoid.AutoRotate = false
local cameraAngleX = 0
local cameraAngleY = 0
local function playerInput(actionName, inputState, inputObject)
-- Calculate camera/player rotation on input change
if inputState == Enum.UserInputState.Change then
cameraAngleX = cameraAngleX - inputObject.Delta.X
-- Reduce vertical mouse/touch sensitivity and clamp vertical axis
cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
-- Rotate root part CFrame by X delta
rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
end
end
ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
event = RunService.RenderStepped:Connect(function()
if camera.CameraType ~= Enum.CameraType.Scriptable then
camera.CameraType = Enum.CameraType.Scriptable
end
local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -10000))
camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
end)
end
setCam()
player.CharacterAdded:Connect(function(character)
setCam()
end)
local function focusControl(actionName, inputState, inputObject)
-- Lock and hide mouse icon on input began
if inputState == Enum.UserInputState.Begin then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
end
end
ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
end)
script.Parent.Unequipped:Connect(function()
camera.CameraType = Enum.CameraType.Custom
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
event:Disconnect()
ContextActionService:UnbindAction("PlayerInput")
mouse.Icon = ""
player.Character:WaitForChild("Humanoid").AutoRotate = true
end)