OTS Camera Issue

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.

https://streamable.com/b48551

https://streamable.com/m5bpr7

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?

Bumping this topic because I haven’t gotten a reply, and I’ve already made the same post days ago and it also didn’t get a reply. I still need help.

1 Like

last bump before i delete this, i still am in need of help

It points to that specific direction because that’s the last known camera CFrame during OTS mode. When you disable OTS, it still remembers that CFrame. Whenever you reenable the OTS, you should update the CFrame to reflect the current camera angle, instead of it restoring to its own previously known one

I can’t give you an exact solution, but this part here should be relevant:

Basically, when you reenable the OTS, you need to update these numbers to match the current camera CFrame. Below is how I did it; it’s not perfect, but it should be good enough:

		if newToggleState then
			--this part runs when the OTS is toggled ON
			uis.MouseBehavior = if newUnlockMouseState then Enum.MouseBehavior.Default else Enum.MouseBehavior.LockCenter
			cam.CameraType = Enum.CameraType.Scriptable	
	
			local _, x, y: number = workspace.CurrentCamera.CFrame:ToOrientation()
			module.X, module.Y = math.deg(-x), math.deg(-y) --update the rotation values
		else
			--this part runs when the OTS is toggled OFF
			uis.MouseBehavior = Enum.MouseBehavior.Default
			cam.CameraType = Enum.CameraType.Custom
			cam.FieldOfView = module.FOV
		end	

This is just for reference on how my OTS uses the rotation values. Yours should look similar

This is what my OTS looks like before the change; it suffers the same problem as yours:


And here’s what it looks like after the change:

1 Like

How would I go about changing this if I don’t have a module, what does it do?
Im new to scripting so Im sorry if this sounds like a dumb question

It literally doesn’t matter. I used a module because of organization reasons. You just need to change those X and Y values to match the current camera CFrame when the OTS mode is toggled on

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.