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.
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).
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.
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.
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
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.
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.
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
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.
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
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.