Exiting out of first person mode forces mouse to be unlocked

Hello guys, today I’m having a bit of an issue concerning mouse lock. I have a script set up so that with the press of a key, you can toggle between roblox’s default first person mode, and a custom third person mode. The issue is that even with me making it so that it locks the mouse, it still forces the mouse to be unlocked, which practically disabled my third person view system. (Atleast I believe that’s the issue)

Here’s the block of code that’s relevant to my problem:

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.V then
		if firstPerson == false then
			firstPerson = true
			Camera.CameraType = Enum.CameraType.Custom
			localPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
		else
			firstPerson = false
			Camera.CameraType = Enum.CameraType.Scriptable
			localPlayer.CameraMode = Enum.CameraMode.Classic
			userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		end
	end
end)

You can see here that if you press the ‘V’ key, it uses roblox’s default first person mode, and if you press it again, it lets my custom third persion view take control over the camera.

84b621a18b4ed913a136d1d5db5edc6f
You can see here in the gif what I’m experiencing.

Any ideas on how to solve this issue? Thanks

Quick question, did you zoom out yourself or it is scripted to zoom out when you exit first person?

It automatically zooms out when you change the cameraMode to classic. I prevent the player from zooming into first person at all. Why do you ask?

Make it so, after you zoom out, lock the mouse, because while you zoom out the MouseBehaviour turns to default since it’s Classic camera mode. Try that.

1 Like

Didn’t you look at my code snippet? It tries to lock the mouse after it changes the mode, yet it still fails.

You said it automatically zooms out right? (zooming to third person)

Yes it does, as a result of just me changing the CameraMode. I did not write anything manually to zoom out, apart from that one line

Just to make sure, to avoid confusion and stuff. Answer me on these 2 questions please.

  1. Did you use your mousewheel to adjust the zooming of the camera?

  2. If no, then could you show me or tell me about the code in your script that zooms the camera OUT (not in).

  1. No, I did not. It switches the camera mode upon key press.
  2. There’s no code that zooms it out apart from switching from first person to classic, and setting the minimal zoom so you can’t zoom into first person. In fact, you can’t zoom at all because of how my camera system works.

That’s what I wanted. At the line of code you set the camera’s zooming boundries (zooming out), you have to add the MouseBehaviour.LockCenter line, right below that, maybe add a wait() incase it is still zooming out after trying to lock it (or wait for the tween to finish etc etc, always depending how’s scripted).

I think you misundestand. The minimal zoom is set in the initialization of the script and is never changed. The only zooming out is done automatically by roblox’s default camera system when changing the CameraMode’s. My problem is that the mouse unlocks, and I’ve tried to fix that by setting the LockCenter in the initialization and during the change.

prob cause lines like this, you dont have to change camera type for first person.

he means that after you do this

wait for some time like RunService.Heartbeat:Wait(), then

userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

Why do you need to wait?

You set the CameraMode to Classic and at the same time, you also set the MouseBehavior to LockCenter. But because of how the default CameraModule works, it sets the MouseBehavior to Default at the NEXT FRAME after you “free” the camera

summary

-- First person
FirstPerson -> set CameraType to Custom -> set MouseBehavior to LockFirstPerson

-- Third person
ThirdPerson -> set CameraType to Classic -> set MouseBehavior to LockCenter
-- next frame
Roblox CameraModule found out CameraType is classic -> check minimum zoom ->
-> zoom is less than minimum so set zoom to minimum -> zoom is more than 0.5 so set MouseBehavior to Default
-- this part is where you need to add the RunService.Heartbeat:Wait()

-- after Heartbeat:Wait()
set MouseBehavior to LockCenter
2 Likes