Weird first person camera behaviour in custom script?

So I’ve tried porting a camera system from one of my old Unity projects into roblox. Now that code was pretty buggy off the bat so I decided to scrap it and make a first person scripted camera for roblox and this is the code i’m using:

RunService.RenderStepped:Connect(function(deltaTime)
		mouseX = mouse.X * deltaTime * Sensitivity
		mouseY = mouse.Y * deltaTime * Sensitivity
		
		xRotation -= mouseY
		xRotation = math.clamp(xRotation, -90, 90)
		
		print(mouse.X)

		camera.CFrame = CFrame.new(character.Head.Position)
		camera.CFrame = (camera.CFrame * CFrame.Angles(math.rad(xRotation), 0, 0))
		character.HumanoidRootPart.CFrame = (character.HumanoidRootPart.CFrame * CFrame.Angles(0, mouseX, 0))
			 



	end)

The result of this code is some extremely weird camera behavior as seen here:

Now I know the problem is caused by roblox’s weird input system (compared to unity) where the coordinates of the mouse start at the very left and bottom (i think) of the screen. I just don’t know any work arounds since there are no tutorials or guides to make these cameras online :frowning:

2 Likes

The player’s Humanoid’s AutoRotate relies on the camera (which is why there’s a 0.5 stud minimum distance between the player’s Head and Camera in first person). With the Camera at the pinpoint center of the Head, the AutoRotate gets bamboozled and spins the player erratically.

If you were to change the behavior of the FPS camera, you’ll probably have to create your own AutoRotate too.

3 Likes

Well I mean at the bottom of the code lies my auto rotate:

character.HumanoidRootPart.CFrame = (character.HumanoidRootPart.CFrame * CFrame.Angles(0, mouseX, 0))

The question is how do I disable it. Also there exists the problem of the weird mouse behaviour. If you look at the clip I sent (You might have to click it), you can see that the mouse doesnt act as a mouse but more a “slider”. I need to worry about that too.

To disable AutoRotate, do

-- Assuming you localized (or assigned to a variable) Players service...
local humanoid = players.LocalPlayer:WaitForChild("Character"):FindFirstChildWhichIsA("Humanoid")

humanoid.AutoRotate = false

You can lock the mouse using the UserInputService. You can set its MouseBehavior to Enum.MouseBehavior.LockCenter so it stays in the center of your screen. This service also provides :GetMouseDelta(), basically the “velocity” of the mouse when it is locked.

2 Likes

Thank you! I was worried that roblox didn’t support this kind of mouse input but it seems like it does exists (phew).

Edit:
@Y_VRN I just have one final issue:

The camera doesn’t seem to want to rotate with the HumanoidRootPart despite me having this in its rotation code:

		camera.CFrame = CFrame.new(character.Head.Position)
        -- This should work right?
		camera.CFrame = (camera.CFrame * CFrame.Angles(math.rad(xRotation * Sensitivity), math.rad(character.HumanoidRootPart.CFrame.Rotation.Y), 0))
		character.HumanoidRootPart.CFrame = (character.HumanoidRootPart.CFrame * CFrame.Angles(0, mouseX, 0))
1 Like

Nevermind. I fixed it with maths!

		camera.CFrame = CFrame.new(character.Head.Position)
		camera.CFrame = (camera.CFrame * ((character.HumanoidRootPart.CFrame.Rotation * CFrame.Angles(math.rad(1), math.rad(-1), math.rad(1))) * CFrame.Angles(math.rad(xRotation * Sensitivity),0, 0)))
		character.HumanoidRootPart.CFrame = (character.HumanoidRootPart.CFrame * CFrame.Angles(0, -mouseX, 0))

May have room for improvement but im happy with this :slight_smile:

1 Like

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