I am currently having trouble with my OTS camera system. The camera activates whenever the gun is equipped and right click is also held down, the camera deactivates whenever the gun is unequipped or when right click input is ended. This is how my camera system works, however there is a problem. Why is it that whenever I right click to activate the camera, the camera ends up pointing to a specific direction on the map, the video shows a more clear description of this.
Here is the part of the code that activates & deactivates the camera:
tool.Equipped:Connect(function()
GunIdleTrack:Play()
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
GunIdleTrack:Stop()
AimGunTrack:Play()
local character = tool.Parent
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local cameraAngleX = 0
local cameraAngleY = 0
humanoid.AutoRotate = false
local function playerInput(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Change then
cameraAngleX -= inputObject.Delta.X
cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)
end
end
ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
cameraUpdateConnection = RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -1000000))
camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)
local lookingCFrame = CFrame.lookAt(rootPart.Position, camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -10000000000)))
rootPart.CFrame = CFrame.fromMatrix(rootPart.Position, lookingCFrame.XVector, rootPart.CFrame.YVector)
local function focusControl(actionName, inputState, inputObject)
camera.CameraType = Enum.CameraType.Scriptable
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIcon = "http://www.roblox.com/asset/?id=10181263678"
end
ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton2)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
GunIdleTrack:Play()
AimGunTrack:Stop()
ContextActionService:UnbindAction("PlayerInput")
RunService:UnbindFromRenderStep("CameraUpdate")
ContextActionService:UnbindAction("FocusControl")
ContextActionService:UnbindAction("Reload")
camera.CameraType = Enum.CameraType.Custom
humanoid.AutoRotate = true
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
UserInputService.MouseIcon = ""
end
end)
end)
end
GunMenuScreenGUI.Enabled = true
tool.Activated:Connect(function()
local mousePosition = mouse.Hit.Position
local function ClientFire()
local hum = char:WaitForChild("Humanoid")
if not HasShot and ammoLeftVal.Value > 0 then
HasShot = true
FireRemote:FireServer(mousePosition)
ammoLeftVal.Value -= 1
RecoilAnimTrack:Play()
TweenAdd:Play()
TweenAdd.Completed:Connect(function()
TweenRevert:Play()
end)
task.wait(FireRate)
HasShot = false
elseif ammoLeftVal.Value == 0 then
print("Player has no ammo left")
elseif HasShot then
print("Player is currently still shooting.")
end
end
ClientFire()
end)
ContextActionService:BindAction("Reload", function(name, state, obj)
if state == Enum.UserInputState.Begin then
reload()
end
end, false, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
if AimGunTrack and RecoilAnimTrack then
AimGunTrack:Stop()
RecoilAnimTrack:Stop()
GunIdleTrack:Stop()
end
GunMenuScreenGUI.Enabled = false
ContextActionService:UnbindAction("PlayerInput")
if cameraUpdateConnection then
RunService:UnbindFromRenderStep(cameraUpdateConnection)
cameraUpdateConnection = nil
end
ContextActionService:UnbindAction("FocusControl")
ContextActionService:UnbindAction("Reload")
camera.CameraType = Enum.CameraType.Custom
hum.AutoRotate = true
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
UserInputService.MouseIcon = ""
end)
end)
I don’t understand why the camera ends up pointing to this very specific direction on the map whenever the camera is right clicked, my goal is to make the camera point to where the mouse actually is. The camera is handled through runservice:bindToRenderStep & ContextActionService, then when the gun is unequipped OR the user has stopped holding down right click, both functions unbind using the “:UnbindAction” and “UnbindFromRenderStep”.
Can somebody tell me how to make the camera angle actually point to the mouse, and also tell me why the camera currently points to this very specific point in the map?