Car camera perspectives

I already have a car chassis with a camera that is always a few studs behind the car. I want to create a camera angle like this when the car turns (you can see part of the side of the car). Does anybody know how to do this? Thanks.

1 Like
-- Assume that carChassis is the car chassis with a camera attached
local camera = carChassis.Camera
local previousOrientation = carChassis.CFrame
local cameraOffset = Vector3.new(0, 2.5, -6) -- How far the camera is from the car
local cameraLookAtOffset = Vector3.new(0, 1.5, 0) -- Where the camera looks at relative to the car
local cameraDistance = 10 -- How far the camera is from the car

-- Camera update function
local function updateCamera()
	-- Calculate the difference in orientation
	local currentOrientation = carChassis.CFrame
	local orientationDiff = previousOrientation:ToObjectSpace(currentOrientation)

	-- Calculate the camera position and look-at point
	local cameraPosition = currentOrientation:PointToWorldSpace(cameraOffset)
	local lookAtOffset = orientationDiff:VectorToWorldSpace(cameraLookAtOffset)
	local lookAtPosition = cameraPosition + lookAtOffset

	-- Calculate the camera distance based on the car's speed
	local carVelocity = carChassis:GetVelocity()
	local carSpeed = carVelocity.Magnitude
	local targetDistance = math.clamp(carSpeed / 10, 5, cameraDistance)
	local cameraDistance = math.lerp(cameraDistance, targetDistance, 0.1)

	-- Set the camera position and rotation
	local cameraDirection = (lookAtPosition - cameraPosition).Unit
	cameraPosition = lookAtPosition - cameraDirection * cameraDistance
	camera.CFrame = CFrame.lookAt(cameraPosition, lookAtPosition)

	previousOrientation = currentOrientation
end

-- Call updateCamera every frame
game:GetService("RunService").RenderStepped:Connect(function()
	updateCamera()
end)