Locking player's camera to a part

This might be simple, but as a beginner scripter, I gotta start from somewhere.

What I’m trying is to lock the player’s camera to a specific part and when the part moves, the camera does as well. (The camera will follow the part, exactly to as Getting Over It works). I’ve tried looking through Dev hub articles regarding this but it’s an overall confusion.

Please link me or help me with creating this code for the player locking to a part.

Thank you

12 Likes

You can use the CameraSubject property for this. I believe if you set the CameraSubject to the part, it’ll follow it automatically (although you may have to play around with the CameraType as well, I’m not sure if the default works for it or not).

1 Like

The camera will not be rotatable. 2d theme

2 Likes

Like what @ThomasChabot said, you can use the CameraSubject property for following the part. you will need to set up a code to something like this:

local camera = workspace.CurrentCamera
camera.CameraSubject = workspace.part -- find the part's path

From there you would have to change the CameraType depending on how you want it to follow the part, yes the default “Custom” works with this, but there are some others you can play with that work like follow.

Here is a camera article for more information: Camera | Documentation - Roblox Creator Hub

7 Likes

Would that be in StarterPlayerScripts?

1 Like

you can put it in a local script about anywhere you like, btw the only way to follow the camera that is closest to not having the camera be able to rotate is changing it to “attach” otherwise you would have to change it to multiple CameraTypes.

1 Like

I set it in Workspace.

To make it as workspace you can do something like this i guess? (Not tested)

local CameraObject = Part

local Cam = game.Workspace.CurrentCamera
Cam.CameraSubject = game.Workspace:FindFirstChild(CameraObject)
Cam.CameraType = Enum.CameraType.Scriptable

I don’t know if that works.

3 Likes

he needs the camera to follow the part, changing the CameraType to “Scriptable” will not move the camera to the part all of the time, since it stays in that one spot it was first put into.

How do I set a custom angle (2d) for the camera to show. I want to disable zooming in/out as well.

Basically with the part that the camera will focus on will be working as the “torso” or “player”.

you would have to always change the position so the camera follows the part each time, but setting the CameraType to “Scriptable” should keep it 2d.

1 Like

Most people have already given a good answer but to elaborate.
You need to use either runService or the method :GetPropertyChangedSignal(“Position”) to see whenever the part is moving.

15 Likes

Then you can use CFrame to the camera.

Here is a script I prepared for you that would work for this:

[TESTED]

    local camera = workspace.CurrentCamera -- finds the camera
    local Run = game:GetService("RunService")
    local part = workspace.CameraView -- name of my camera part
    camera.CameraType = Enum.CameraType.Scriptable
    camera.CameraSubject = part

    local function OnChanged()
        camera.CFrame = part.CFrame
        wait()
    end
    Run.RenderStepped:Connect(OnChanged) -- checks for when part moves

Here is an article on RenderStepped to help you understand some more: RunService | Documentation - Roblox Creator Hub

Here is one on GetPropertyChangedSignal as well, but as @Conejin_Alt said, it would create more lag then runservice:

(Mark this as a Solution (:white_check_mark:) if it works, any more information on this, you can tell us and we can help)!

21 Likes

I recommend you use RenderStepped instead, i think that produces less lag.

3 Likes

That is actually not true, heartbeat is better than renderstepped as it causes less work for the computer.

2 Likes

I have not mentioned HeartBeat, but i thought that HeartBeat is only updating in every second. RenderStepped is updating in every frame changed.

2 Likes

Heart USED to work similarly to what you described (30 Hz) but now it changes to whatever your game’s framerate is at exactly like Renderstepped. The fundamental difference between heart and Renderstepped is that, the latter fires before each frame, whilst heart fires after which will significantly improve performance.

3 Likes

Hello, @qEvann. I had a question like this a little while ago, and solved it using run service. I was cframing the camera, but it had choppy movement. I ended up using camera:Interpolate. Here’s a link to it: Help with Custom Camera Movement - #12 by PlayAsync

Depending on how you’re moving the part, this might be a smoother method.

Good luck with your 2d game/project!
Someperson576

2 Likes

Correct , also each frame heartbeat fires, it depends on the local machine’s performance and capability. Not to mention heartbeat fires after physics simulation. Using Heartbeat would be more appropriate here.

5 Likes