[CODE SNIPPET] Force First Person on Drive By

I’ve been looking for a script or a way to force First Person on players when they are doing a drive-by as the driver but couldn’t find anything.
So I spent the last hour trying different methods and functions and I finally got it to work.

As I believe there is no publicly released script that can achieve this purpose, I wanted to share my code with the community :smiling_face_with_three_hearts:

  • I also made it so when the player stops doing the drive-by it’ll switch him back to the view cam that he had before.

So, for this to work, put this code on the client side :smile:

------------------------------------------------------
---------  Forced First Person On Driveby  -----------
------------------------------------------------------
local recentViewMode = 0
local changedViewMode = false

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(10)
		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped) then
			local veh = GetVehiclePedIsIn(ped, false)
			if GetPedInVehicleSeat(veh, -1) == ped and IsPedDoingDriveby(ped) then
				if GetFollowVehicleCamViewMode() ~= 4 then
					recentViewMode = GetFollowVehicleCamViewMode()
					local context = GetCamActiveViewModeContext()
					SetCamViewModeForContext(context, 4)
					changedViewMode = true
				end
			end
			if changedViewMode and not IsPedDoingDriveby(ped) then
				local context = GetCamActiveViewModeContext()
				SetCamViewModeForContext(context, recentViewMode)
				changedViewMode = false
			end
		end

	end

end)

Tebex

9 Likes

there now way u can make it to where the hang out the window and do drive bys like in GTA SA

I don’t think that’s possible with GTA natives but maybe someone can create a custom animation that should make that possible :slight_smile:

1 Like

Can you make it so all players in cars are able to shoot?

Here is a way so all people will go first-person while shooting!

all credits to @aymannajim

------------------------------------------------------
---------  Forced First Person On Driveby  -----------
------------------------------------------------------
local recentViewMode = 0
local changedViewMode = false

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1)
		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped) then
			local veh = GetVehiclePedIsIn(ped, false)
			if GetPedInVehicleSeat(veh, -1) == ped and IsPedDoingDriveby(ped) then
				if GetFollowVehicleCamViewMode() ~= 4 then
					recentViewMode = GetFollowVehicleCamViewMode()
					local context = GetCamActiveViewModeContext()
					SetCamViewModeForContext(context, 4)
					changedViewMode = true
				end
			end
			if changedViewMode and not IsPedDoingDriveby(ped) then
				local context = GetCamActiveViewModeContext()
				SetCamViewModeForContext(context, recentViewMode)
				changedViewMode = false
			end
		end

	end

end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1)
		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped) then
			local veh = GetVehiclePedIsIn(ped, false)
			if GetPedInVehicleSeat(veh, 0) == ped and IsPedDoingDriveby(ped) then
				if GetFollowVehicleCamViewMode() ~= 4 then
					recentViewMode = GetFollowVehicleCamViewMode()
					local context = GetCamActiveViewModeContext()
					SetCamViewModeForContext(context, 4)
					changedViewMode = true
				end
			end
			if changedViewMode and not IsPedDoingDriveby(ped) then
				local context = GetCamActiveViewModeContext()
				SetCamViewModeForContext(context, recentViewMode)
				changedViewMode = false
			end
		end

	end

end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1)
		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped) then
			local veh = GetVehiclePedIsIn(ped, false)
			if GetPedInVehicleSeat(veh, 1) == ped and IsPedDoingDriveby(ped) then
				if GetFollowVehicleCamViewMode() ~= 4 then
					recentViewMode = GetFollowVehicleCamViewMode()
					local context = GetCamActiveViewModeContext()
					SetCamViewModeForContext(context, 4)
					changedViewMode = true
				end
			end
			if changedViewMode and not IsPedDoingDriveby(ped) then
				local context = GetCamActiveViewModeContext()
				SetCamViewModeForContext(context, recentViewMode)
				changedViewMode = false
			end
		end

	end

end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1)
		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped) then
			local veh = GetVehiclePedIsIn(ped, false)
			if GetPedInVehicleSeat(veh, 2) == ped and IsPedDoingDriveby(ped) then
				if GetFollowVehicleCamViewMode() ~= 4 then
					recentViewMode = GetFollowVehicleCamViewMode()
					local context = GetCamActiveViewModeContext()
					SetCamViewModeForContext(context, 4)
					changedViewMode = true
				end
			end
			if changedViewMode and not IsPedDoingDriveby(ped) then
				local context = GetCamActiveViewModeContext()
				SetCamViewModeForContext(context, recentViewMode)
				changedViewMode = false
			end
		end

	end

end)


all people should be able to shoot from the car

That’s very bad performance-wise.
All those threads for something that can be easily checked within the initial thread.
If you want to force First Person on anyone doing a drive by ( not only the driver ), you can do this:

------------------------------------------------------
---------  Forced First Person On Driveby  -----------
------------------------------------------------------
local recentViewMode = 0
local changedViewMode = false

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(10)
		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped) then
			if IsPedDoingDriveby(ped) then
				if GetFollowVehicleCamViewMode() ~= 4 then
					recentViewMode = GetFollowVehicleCamViewMode()
					local context = GetCamActiveViewModeContext()
					SetCamViewModeForContext(context, 4)
					changedViewMode = true
				end
			end
			if changedViewMode and not IsPedDoingDriveby(ped) then
				local context = GetCamActiveViewModeContext()
				SetCamViewModeForContext(context, recentViewMode)
				changedViewMode = false
			end
		end

	end

end)
2 Likes

What do you mean by “all players in cars should be able to shoot” ?
If they can’t shoot from inside the vehicle, you should enable:

SetPlayerCanDoDriveBy(PlayerId(), true)
1 Like

yeah it was really late when I wrote that and I just threw it together real quick but yeah your way is better

where the client location?

put it in any client script

Just to let you know mate yours doesn’t work for me it doesn’t force first person, only one working was the one Twisle dropped above

1 Like

Use this Its better performance

------------------------------------------------------
---------  Forced First Person On Driveby  -----------
------------------------------------------------------
local recentViewMode = 0
local changedViewMode = false

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(10)
		local ped = PlayerPedId()

		if IsPedInAnyVehicle(ped) then
			if IsPedDoingDriveby(ped) then
				if GetFollowVehicleCamViewMode() ~= 4 then
					recentViewMode = GetFollowVehicleCamViewMode()
					local context = GetCamActiveViewModeContext()
					SetCamViewModeForContext(context, 4)
					changedViewMode = true
				end
			end
			if changedViewMode and not IsPedDoingDriveby(ped) then
				local context = GetCamActiveViewModeContext()
				SetCamViewModeForContext(context, recentViewMode)
				changedViewMode = false
			end
		end

	end

end)
2 Likes

I see that you misunderstood what my code does.
My initial script only works for the driver.
The second code here does it for all the passengers of that vehicle:

got ya! that is the one i tried and it didnt work bud.