Formula for changing Humanoid CameraOffset relative to head CFrame

Hey guys,

I’ve been working on a firearms system for my game for a while now. I’ve gotten the furthest I ever have, and it’s all looking nice.

After implementing a life-saving open-source script that essentially adds a waist joint to R6 characters so I can move my torso up and down to face my mouse, I’ve run into another issue.

See, ROBLOX’s camera doesn’t actually come from your head, and the guns kind of drift out of view when you look and down cause the camera isn’t where it’s intended to be, seen here:

Before implementing that the waist move script, I had only needed to change the CameraOffset in Humanoid to Vector3.new(0, 0.5, 0) just so that the guns didn’t block a lot of the player’s vision.

I’ve never been good at math or things that require a lot of brainpower. So I’m asking for help with getting the formula to change the CameraOffset and keep it at a constant offset (0.5 studs above the head) as the head moves. I’ve tried messing around with getting the local space of the head relative to the torso, and doing some math stuffs with that, but it definitely didn’t give me what I wanted.

Thanks in advance, as always.

5 Likes

I believe you can just set the camera’s CameraSubject to the character’s head and the camera will follow relative to the head instead of the root

2 Likes

I actually really thought this would work, but it ended up giving me some really buggy behavior.

Good call anyway though.

The best way to solve this problem takes a bit of work. What you’re doing now will look good for other players, but for the local player, you need to do something completely different.

@hellish 's solution might have some bad results. Since your head moves, looking up and down can be a bit disorienting for the player if it seems like they are moving around when looking up and down.

The best solution is to either create new arms, or modify the current arms when you are in first person mode to rotate with your camera.

It seems you already know how to get the Camera CFrame. You simply need to CFrame the arms with an offset from the Camera CFrame. For example:

RightArm.CFrame = CameraCFrame*CFrame.new(-5,-1, 1)*CFrame.Angles(math.rad(90),0,0) 

This is done in a render loop.

This solution makes a lot more work, but a much better end result. This is actually how Phantom Forces does it and many other proper FPS games.

2 Likes

Thanks for the help, but I’ve tried to go the viewmodel method route before and I was confused out of my mind. The whole reason I’m doing it this way is for simplicity sake.

I managed to get the formula on my own. Quite simple now that I look at it.

local objectSpace = (Character.HumanoidRootPart.CFrame + Vector3.new(0, 1.5, 0)):ToObjectSpace(Character.Head.CFrame)
Character.Humanoid.CameraOffset = objectSpace.Position
11 Likes

I just wanna say thanks for doing the math and posting it for the rest of us to use, especially with the recent first-person games coming out.

1 Like

Hey man, no problem. It’s a bit of an unconventional method to utilize, and you wont see any professional ROBLOX games with it, but it’s a good option for newbies and anyone looking to throw this code into a simple gun-inclusive game. However, I would recommend looking into better alternatives for the future.

The only alternatives that I’ve found include completely reworking first person camera in general, I wouldn’t say that your method is bad, it just needs a little ‘smoothing’ is all.