I want the camera to follow the head, so if the character rolls or slides the camera will follow both the location and rotation of the head. I am making a parkour system for a game, that is the reason I would like this.
You can maybe try attaching a motor6d from the camera to your head, but i dont know if it will work
–tbh im completely new–
Ah, well, thank you for attempting to help; I may just make it match the location but not the rotation, I guess. Since you’re new, if you need any help let me know.
wait before we finish, could i see your code?
Do you mean in a position where, say if you rolled with this camera locked onto the head, your camera would literally roll with you and possibly induce dizziness?
(Kind of like the realistic ragdoll simulator FPS optins you find)
You can just put the head instead of this newpart creating and stuff.
local centerHeadAttach: Attachment = script.Parent:WaitForChild("Head"):WaitForChild("FaceCenterAttachment")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local runServ = game:GetService("RunService")
local currentCamera = workspace.CurrentCamera
local newCameraPart = Instance.new("Part")
local newAttach = Instance.new("Attachment", newCameraPart)
local newPosAlign = Instance.new("AlignPosition", newCameraPart)
local newRotAlign = Instance.new("AlignOrientation", newCameraPart)
newPosAlign.RigidityEnabled = true
newPosAlign.ReactionForceEnabled = true
newPosAlign.ApplyAtCenterOfMass = true
newPosAlign.Attachment0 = newAttach
newRotAlign.Attachment0 = newAttach
newPosAlign.Attachment1 = centerHeadAttach
newRotAlign.Attachment1 = centerHeadAttach
newCameraPart.CanCollide = false
newCameraPart.Size = Vector3.new(0.5, 0.5, 0.5)
newCameraPart.CFrame = centerHeadAttach.WorldCFrame
newCameraPart.Parent = script.Parent
--currentCamera.CameraSubject = newCameraPart
currentCamera.CameraType = Enum.CameraType.Scriptable
runServ.RenderStepped:Connect(function(dt)
currentCamera.CFrame = newCameraPart.CFrame
currentCamera.CFrame = CFrame.lookAt(currentCamera.CFrame.Position, mouse.Hit.Position)
end)
Yes, exactly this. I have a working system right now, but it makes everything jitter when you walk backwards or sideways. I wouldn’t have any animations extreme enough to induce motion sickness though, and I will add a setting to disable it.
This does somewhat get the effect that I am looking for, but it makes the mouse not be locked while in first person, it also makes the character turn oddly while pressing A or D.
You could try this code:
-- Put in StarterCharacterScripts
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local character = script.Parent
local head = character:WaitForChild("Head") -- Pretty sure this is the name of the head part
local camera = Workspace.CurrentCamera
local function afterCamera(delta)
-- TODO: Make sure the head cframe is where you want it
-- i.e. use camera.CFrame to reposition the head properly
-- e.g. head.CFrame = camera.CFrame - camera.Position + head.Position
camera.CFrame = head.CFrame
end
RunService:BindToRenderStep("After camera", Enum.RenderPriority.Camera.Value + 1, afterCamera)
This will override the camera to the CFrame of the head. To have the camera still move you’ll still need to move the cframe of the head, but you can use the modified camera CFrame from the last update as shown in the code above. (Also make sure to have the camera set to first person in StarterCharacter or in the Player object.)
This is useful for capturing the default camera movement but not being locked into the exact position determined by the default camera.
This does technically put the camera in the right position, but it also makes you randomly turn as you’re walking. I’m not really sure how to do this properly. Thank you for the help so far though.
I accidentally had a minus one instead of a plus one for the priority. The change might have fixed it, though I suspect the problem might be something different (you might need to make the movements character relative instead of camera relative, though I don’t recall the specifics of doing that).
Edit:
Here is how to make the character movement relative to something other than the camera. That should fix your problem if you have something in mind for it to move relative to.
(I also realize now this might not be what you meant by turning. Perhaps the fixed code will solve that problem.)
Thank you, I will test this and edit this reply when I know if it works.
edit 1:
What I mean by the turning and things is that it behaves like you’re in third person. It turns left while holding A and right while holding D. Another issue is that the mouse isn’t locked to the center of the screen, so you can only turn by using A or D.
Did you make sure to set the camera to first person? It will look like first person either way but the camera movement behavior is inherited from the mode it’s in.
You can set the camera to first person by adding:
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
player.CameraMaxZoomDistance = 0
So, making the movement relative to the character instead of the camera would be great, but the mouse no longer rotates the character (since it isn’t locked) so I wouldn’t be able to turn at all after spawning in.
Ah, sorry that was a stupid mistake. It now spins rapidly (because of the camera and the head creating a loop?) I’m not completely sure why. Even if I don’t move the mouse it will spin like crazy for every few seconds. Would it be better to just throw out the idea of making it follow the rotation? Should I make it only follow the position instead? Then main reason I even got into this whole camera positioning mess is because of my running animation, it makes the player lean forward a bit and if I just make the body visible the camera is too far back and too high.
I’m working on some code though it’s more difficult than I thought. I’ll see if I can get something working.
Thank you, I’ve been trying to get this to work for about 5-6 hours since yesterday haha. I finally decided to make a post and it seems that I’m not the only one who finds it to be somewhat difficult. Thanks for the help.
Here is some code for this:
-- Put in StarterCharacterScripts
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local character = script.Parent
local head = character:WaitForChild("Head")
local neck = head:WaitForChild("Neck")
local hrp = character:WaitForChild("HumanoidRootPart")
character:WaitForChild("Humanoid").AutoRotate = false
local camera = Workspace.CurrentCamera
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
-- Set the camera to first person
player.CameraMaxZoomDistance = 0
local beforeCFrame = camera.CFrame
local function afterCamera(delta)
-- Calculate the amount to rotate the hrp from the camera input
local beforeAngles = beforeCFrame - beforeCFrame.Position
local afterAngles = camera.CFrame - camera.CFrame.Position
local rx, ry, _ = beforeAngles:ToObjectSpace(afterAngles):ToEulerAnglesXYZ()
hrp.CFrame = hrp.CFrame * CFrame.Angles(0, ry, 0)
-- Calculate the amount to have the head look up or down
neck.C0 = neck.C0 * CFrame.Angles(rx, 0, 0)
-- Basic (Follows head exactly)
--camera.CFrame = head.CFrame
-- No tilt (Makes it so the camera doesn't tilt)
camera.CFrame = CFrame.lookAt(head.Position, head.Position + head.CFrame.LookVector)
-- Might need some code to limit the angle around looking exactly up and down otherwise there might be some character rotation
end
local function beforeCamera(delta)
beforeCFrame = camera.CFrame
end
RunService:BindToRenderStep("Before camera", Enum.RenderPriority.Camera.Value - 1, beforeCamera)
RunService:BindToRenderStep("After camera", Enum.RenderPriority.Camera.Value + 1, afterCamera)
You might need some specific animations to reduce the bounciness (the head moves around a bit while walking with the normal animations).
You could also add some smoothing/lerping to reduce the bouncing.
It’s working better, but it still spazzes out a ton. Thanks for the help, I may just follow the position and not the rotation of the head. This is kinda complicated and doesn’t want to work right so far. Thanks again for the help. Does it work properly for you? if it does and doesn’t start spinning than maybe it has to do with one of my other scripts, I could change them if needed.
Edit:
Just tested it in a blank game and it works, I’ll have to see what’s interfering. It does make the camera Y movement inverted though, any idea why it’s doing that and/or how to fix it?
Thank you! Now I just have to find what is interfering with it and fix it, also, just in case anyone else finds this solution. The neck variable should be ‘local neck = torso:WaitForChild(“Neck”)’
Just make sure to have it looking in the torso not the head.