Playing horn for a vehicle that has a player inside?

Hello there!

I’m attempting to re-create Los Santos Customs and it was going well up until I decided to add the trick to preview vehicle modifications. I have managed to work it out for suspension, paint jobs, etc. but I can’t figure out how to sound the horn for long enough when there is a player in the driver’s seat.

I have attempted to create an external vehicle then attach the horn to be previewed to the new vehicle and sound it that way to no avail. In doing that, I also attempted to create a ped and spawn it in the vehicle to see if that would help but also didn’t.

I have attempted to warp the player out of their vehicle, sound the horn, then warp them back in but again, no avail.

If anyone has any ideas, I will be more than grateful because I have spent the last hour or so scouring through Google :smile:

2 Likes

Did you try SoundVehicleHornThisFrame(veh) native?

1 Like

Yep, and I just tried it again to confirm. It works fine for the shorter horns (stock, police, truck, clown, etc.) but when it comes to the musical loops and whatnot, it doesn’t play them at all because of how short it plays the horn for.

1 Like

Did you try running it in a loop for x seconds?

2 Likes

Just tried it and has the same result. Works for the shorter horns (even though it only plays for < 1 second) but the longer horns don’t even get played.

1 Like

I’ve actually just had a different idea. I went ahead and tried to warp the player out of the vehicle, and by adding some delays, got it to work. My current idea is to make it look nice and have a fixed camera (like the LSC in GTA:O) that focuses on the vehicle.

Anyone got any ideas on how I would tackle that? Looking at the natives, I can’t find anything that’s fixed so I’d presume it’s going to be complicated.

Disregard: I looked harder through one of the other LSC scripts I found and found out they accomplished near enough the same thing so based my code off of theirs. Everything’s working fine now :smile:

1 Like

I have a cam example code I can send you when I’m home later

Thanks, but I’ve already figured it out thanks to a few other scripts. :slight_smile:

Would you be so kind as to post the solution in case other people have the same question

Of course! :smile:
For playing the horn, I had to teleport the player outside and wait until I had finished playing the horn to then teleport the player back inside the vehicle.

local coords = GetEntityCoords(PlayerPedId())
SetEntityCoords(PlayerPedId(), coords.x, coords.y, coords.z+2.0) --teleport the player up a bit
SetEntityVisible(PlayerPedId(), false) --make the ped invisible so it doesn't look weird
StartVehicleHorn(veh, 5000, GetHashKey("HELDDDOWN"), false) --play the horn for 5000 milliseconds (5 seconds)

As for the camera solution:
To create and activate the new camera view:

local cam = CreateCam('DEFAULT_SCRIPTED_CAMERA', true, 2)
local gameplayCam = GetRenderingCam()
local camCoords = GetCamCoord(cam)
local coords = GetEntityCoords(veh)
PointCamAtCoord(cam, coords.x, coords.y, coords.z)
SetCamActive(cam, true)
RenderScriptCams(1, 0, cam, 0, 0)

To return back to normal camera view:

SetCamCoord(cam, GetGameplayCamCoords())
SetCamRot(cam, GetGameplayCamRot(2),  2)
RenderScriptCams(1, 1, 0, 0, 0)
RenderScriptCams(0, 1, 1000, 0, 0)
SetCamActive(gameplayCam, true)
EnableGameplayCam(true)
SetCamActive(cam, false)
DestroyCam(cam)

I think he ment the horn code, cause that is probably what people search for if they get this topic

1 Like

Updated my solution. Thanks for pointing that out :joy:

nice work

1 Like

so 4 years later i encountered the smae problem, wanted to horn the car while a person is inside,
i found it out just try this snippet

local count = 100
				while count > 1 do
					SetControlNormal(0, 86, 1.0)
					Wait(1)
					count = count - 1
				end
				StartVehicleHorn(vehicle, 4000, "HELDDOWN", true)

its stolen from renzu_customs all credit to him