How to make a camera follow a vehicle or a player system?

What I want to do here is to make the camera follows the vehicle or a player whenever you drive somewhere it will automatically follows you. In default, the camera will follows you however, the thing is that you can rotate your camera in every angles. I don’t want this to happened. I wanted to disable that option but I don’t really know how to get it working. Take a look at the game called Asphalt 8 or any other racing games in general. The camera will follows the player whenever you go. I was trying to remake that system into Roblox for so long but since my scripting knowledge wasn’t that great, I always find myself in a struggling situations. I’m not an intermediate or a pro scripter as myself. I would love to know it would be possible to make that system like that in a Roblox game. None of any tutorials cover this topic yet. Please let me know if it’s possible. I really wanna know what would it look like in a script. Thank You!

You could accomplish this by changing the player’s Camera type to Scriptable and then in RenderStepped you would update the Camera’s CFrame to the location of the vehicle with an orientation so the camera knows what direction to look. The best way to do this is to have an invisible camera part in the vehicle area you want the player to see from with its LookVector facing the direction you want the player to look in. This part will be part of the vehicle so it should move with it which means welding it to one of the car parts, probably the seat (for interior view) or above the roof (for exterior view). And when the player leaves the vehicle you will disconnect the RenderStepped connection and position the camera back to the player’s head location (with an offset so the camera isn’t inside the head). Change the CameraType to Custom and it will give control back to the player.

1 Like

I see. So that’s how it works. Thanks for breaking it down to me. I appreciated it but I have a question. How do you do it with the script? What type of script do I use to make it works?

Camera is Client sided so you would do this with a LocalScript.

Oh I got it. Sorry if this is dump of me to ask you but I’m not really that great at scripting by myself. If you know much scripting knowledge, would you mind showing me how you do it? I mean it’s up to you. You can refuse it whatever you want

Sure, one moment. I will edit this comment with an example script, give me a minute.

Edit: Here is your example! :smiley:

local Camera = workspace.CurrentCamera

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Head = Character:WaitForChild("Head")

local RunService = game:GetService("RunService")

local RunConnection = false

local function updateCamera(object)
	local CF = object.CFrame
	if object.Name == "Head" then
		if RunConnection then
			RunConnection:Disconnect()
			RunConnection = false
		end
		CF = CF + Vector3.new(3.5,0,7.5)
		Camera.CFrame = CF
		Camera.CameraType = Enum.CameraType.Custom
	else
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CFrame = CF
		RunConnection = RunService.RenderStepped:Connect(function()
			Camera.CFrame = CF
		end)
	end
end


-- When player gets in car, use this:
updateCamera(cameraLocationObject)

-- When player leaves car, use this:
updateCamera(Head)
7 Likes

Thank you so so much for the script. I can be able to learn this by myself more just by reading it. Thank you very much!

2 Likes

So basically, CF defined as an object? Where could I define my variable for a vehicle object? Could it be CF is the vehicle object?

CF was just the CFrame for the camera to use, was easier to store it as a variable so we can change it if needed, like I did when the Head is chosen.

Basically your next step is to detect when the player is entering / existing the vehicle and calling the function provided. If doing this from another script, you will need to set up a RemoteEvent or some way to tell this script to change the camera’s object to follow.

When players enter the vehicle there should be an object in the vehicle like I mentioned in my first post in the thread so the camera knows where to go. and simply just passing that object reference to the function provided. Hopefully this is making sense :smiley:

1 Like

I see. I got so many questions to ask but at the same time I don’t wanna waste your time too much so. Just one more. So when the player left the vehicle. The script would detect the player head then the camera would set it back to default camera mode again right?

1 Like

Yea when setting the Camera mode back to Custom (from Scriptable) it releases control back to the default which will allow player to move around again.

Also I just realized instead of using a RemoteEvent, you can turn this into a ModuleScript and just require() this in your vehicle script and just pass the player’s Head when they get out.

1 Like

Got it! Thank you for your time. Maybe I can just add you and we could just talk there or maybe you don’t have to so it’s fine either way. It would be nice to have a developer friend

1 Like