How do I change the CameraSubject

Hey guys, I’m trying to make a plane, one of the features will be the players camera locking and staying at the back of the plane like so:

I tried setting the camera subject to a part at the back of the plane but I’m not sure what I’m doing wrong.

Here’s my code:

local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera


function onSeated(isSeated, seat)
	if isSeated then
		
		print(seat.Name)
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CameraSubject = seat.CamPart
	end
end

Humanoid.Seated:Connect(onSeated)

I’m not getting errors in the output and the seat name is printing, also as you can see in the video Its changing the camera but the camera is not attaching to the cam part for some reason.

If there is a better way than CameraSubject let me know!

Any help appreciated!

You can probably do this as well, I’m sure that there’s an easier solution to this though.

local RunService = game:GetService("RunService")
local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera

local func
function onSeated(isSeated, seat)
	if isSeated then
		print(seat.Name)
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CameraSubject = seat.CamPart
        func = RunService.RenderStepped:Connect(function()
            Camera.CFrame = CFrame.new(seat.CamPart.Position + Vector3.new(10, 5, 0))
        end)
	end
end

Humanoid.Seated:Connect(onSeated)

What I just did was import the RunService model and then begin an incredibly fast running loop using the .RenderStepped event and then inside that loop I would assign the CFrame of the Camera to 10 studs on the X-axis and 5 studs on the Y-axis away from the campart.

If you’d like to stop the loop in any scope, you can notice that I also assigned the loop to a variable. This way you can just check if the variable is nil or not, and then disconnect it like this and the loop will stop running:

if func then
    func:Disconnect()
end

I am mentally ill as I do not know the difference between the X axis and the Z axis so choose between these two.

you might need to change the cameratype, try messing with it, and see what improves it better.

If you try to change the CameraType with only one line, it will not work as expected, try using this instead.

repeat
      wait()
      Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable

Also, Its better to use RunService to keep the Camera’s CFrame as the Part’s CFrame.

okay to add on this, you can go ahead and remove the loop, it’s very useless as you can add a weld constraint and it’ll do the same thing and it’ll preform better.

What do you mean by that? That means the loop is useless?

A weld constraint is better for this, you don’t need the loop to keep positioning.

The loop means setting the CameraType to Scriptable, which he forgot to since trying one time does not work very well, and not setting the CFrame.

If you talking ab the RunService thing, yeah using a WeldConstraint is better tho, but RunService is kinda more efficient.

i was commenting on a script of some others.

I used run service and CFrame like you said, but the camera is looking straight ahead as opposed to following where the CamPart is looking:

You change the coordinates according to however you want it to, as I said, the coordinates I put in may be incorrect. So just change up the axis values.

Just use weld constraint, it’s more efficient and less laggy. Plus it’ll fix that issue that’s it’s not turning.

Weld constraint is probably a good idea, I just remembered that you also need to set the rotation of the camera.

The issue isn’t the CameraSubject but rather your misunderstanding of how CameraType works. Changing the CameraType will change how the camera will function. So for example, you’re setting the CameraType to “Scriptable” taking a look at the documentation for CameraType you can see that the summary for Scriptable is “No default behavior. Used when developers need to script custom behavior.” Your issue is that you aren’t scripting any custom behaviour so the camera remains in a single position.

I’d recommend changing the CameraType to “Attach” as I think that will replicate the behaviour you’re trying to achieve. But play around with the different CameraTypes and see which one best fits your purpose.

1 Like

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