How to make a vehicle camera

I would like to make it so the camerasubject is set to a part when the player sits down in a vehicle seat but im not sure how to do that, any help?

3 Likes

Camera | Roblox Creator Documentation mentions about the CameraType | Roblox Creator Documentation Property.

I’m not going to fully explain, but you would have to have a Part to act as the camera, then after somehow detecting that a player is sitting in the vehicle, set the player’s CameraType to Scriptable and the player’s camera CFrame to the CFrame of the part.

Gui = script.Parent.Gui
local CurrentCamera = workspace.CurrentCamera
local campart = script.Parent.Parent.CamPart

function onChildAdded(Child)
	if Child.Name == "SeatWeld" then
		if Child.Part1.Name == "HumanoidRootPart" then
			local player = game.Players:GetPlayerFromCharacter(Child.Part1.Parent)
			print("Player:", player)
			if player ~= nil then
				Gui.Parent = player.PlayerGui
				print("Setting camera subject...")
				CurrentCamera.CameraType = Enum.CameraType.Follow
				CurrentCamera.Focus = campart.CFrame
				print("done")
			end
		end
	end
end

function onChildRemoved(Child)
	if Child.Name == "SeatWeld" then
		if Gui ~= nil then
			Gui.Parent = script.Parent
			CurrentCamera.CameraType = Enum.CameraType.Custom
			CurrentCamera.Focus = campart.CFrame
			wait(0.1)
		end
	end
end

script.Parent.ChildAdded:connect(onChildAdded)
script.Parent.ChildRemoved:connect(onChildRemoved)

I have this going so far “done” prints but the camera isn’t being set, it’s still with the player.

1 Like

Check out this solution, it worked for me… just change the camera.subject to the part you want instead of the humanoid:

That just fixed the odd roblox vehicle camera movement for me. Im trying to change the camerasubject when the player sits down.

Instead of humanoid, replace it with the part you want it to focus on.

I made my local script run in the vehicle and it still doesn’t work :confused:

local player = game.Players.LocalPlayer
local character = player.Character -- the player character
local humanoid = character:WaitForChild("Humanoid")
local camPart = script.Parent.Parent.CamPart


function onSeated(isSeated, seat) -- when the player sits
	if isSeated then
		print("I'm now sitting on: " .. seat.Name .. "!")
		game.Workspace.CurrentCamera.CameraSubject = camPart --Set the camera to the player, instead of the vehicle
	else
		print("I'm not sitting on anything")
	end
end

humanoid.Seated:Connect(onSeated) -- when the player sits, run the function

Have you tried setting the camera type to Enum.CameraType.Custom or Enum.CameraType.Scriptable? Instead of Follow.

Also, unless there is an error in your code, any print commands will still print.

Run it from StarterCharacterScripts.

When you say “doesn’t work” you have to be specific. We know the camera isn’t working properly, but what output do you get printed?

If there’s no output then the script isn’t getting a chance to run.
You shouldn’t be running this section of code when the player sits, it should fire when the seat recognizes that a player is seated and finds the Humanoid of the player. When that happens reset the camera type to Attach if you want it to be focused from behind the car and rotate with it facing forward at all times. You can still keep the Humanoid as the CameraSubject and if you need to change the Offset you can do that.

Yes, I have

print("I'm now sitting on: " .. seat.Name .. "!")
		game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
		game.Workspace.CurrentCamera.CameraSubject = humanoid --Set the camera to the player, instead of the vehicle

They all print when I sit down

“Im now sitting on” prints whenever I sit down. And I want it to “Follow” the part not “attatch”. I also want this script to run whenever the player sits down on a certain vehicle seat because the camparts are in different positions depending on the vehicle.

Okay, so I have looked at one of my train games that has a system that’s identical to what you want to achieve, I do not change the CameraType at all. All I do is change the CameraSubject, this is in a LocalScript. Once I detect that a player is no longer sitting in the vehicle, I change the CameraSubject to the player’s Humanoid.

In my case, whenever a player sits in the train, a GUI is cloned, inside the GUI is a LocalScript which immediately sets the camera of the player to the part:

workspace.CurrentCamera.CameraSubject = VehicleSeatCamera -- A variable

Then if the player leaves the seat or something happens, I simply run this code but change the CameraSubject to the player’s Humanoid:

workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

Hope this helps!

1 Like

It helped… kinda…
The camera subject gets set to the campart when i leave the vehicle seat.
(noot is the seat)

function onSeated() -- when the player sits
		workspace.CurrentCamera.CameraSubject = camPart -- A variable
	end
function NotSeated()
	workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
end

noot.ChildAdded:Connect(function(child)
	if child:IsA(character) then
		onSeated()
	end
end)
noot.ChildRemoved:Connect(function(child)
	if child:IsA(character) then
		NotSeated()
	end
end)

So I’ve checked my script and I use ChildRemoved in conjunction with AncestryChanged which does not use function(child) so don’t add that. Also, it isn’t necessary to do ChildAdded as the script already performs this function when it recognises that a player has sat down.

Alright I see, with your version you parent the character to the seat. while I dont, I managed to get it to work when the player sits down but im not sure how to do it when the player leaves the seat

noot.Occupant.Changed:Connect(function()

onSeated()

end)

I did read up on VehicleSeat.Occupant. To detect whether a player is on the seat, you should do if noot.Occupant then. Then run the function. To detect if a player is not on the seat, do if not noot.Occupant then. Try it and let me know.

2 Likes