[QUESTION] Spawn vehicle GTA Online style

Hi everyone,

I’d like to know if someone has ever achieved to spawn a vehicle like it does in GTA Online?

At the moment, when I spawn a vehicle, if there is another player vehicle in the way, the car will spawn on top of it.
In GTA:O, when you spawn a vehicle (LSC, garages, etc.), there is no collisions for a few seconds and it last as long as your are in another entity.

I first thought that NETWORK_FADE_IN_ENTITY would be enough but it’s only graphical, collisions are still here.

I was also thinking of disabling collisions if an entity is already at the spawn point but as far as I know, there is no way to disable collisions for everything EXCEPT the map (so I would need to check every entity in the future coords, disable collisions for each ones and handle when they are far enough to enable collisions :confounded:…)

So I was wondering, how do you guys deal with this?

1 Like

I’m not at a computer so, I can’t test anything but have you have a look at this native:

https://runtime.fivem.net/doc/reference.html#_0x1A9205C1B9EE827F

Oh yeah sorry, I didn’t mention I already tried this one, it disable collisions with everything so your car is falling trough the map :cold_sweat:

I came up with this when I wanted similar functionality. This will always ensure that the car spawns on a road, but unfortunately I haven’t yet figured out how to offset the position to have it park on the side of the road (as it does in GTAO).

local myPed = GetPlayerPed(-1)
local vehicle = GetHashKey(carName)

RequestModel(vehicle)
while not HasModelLoaded(vehicle) do
  Wait(1)
end

local coords = GetEntityCoords(myPed,true)
local found, outPos, outHeading = GetClosestVehicleNodeWithHeading(coords['x'], coords['y'], coords['z'], 1, 3.0, 0)

if found then
  local spawned_car = CreateVehicle(vehicle, outPos, outHeading, true, false)
  SetVehicleOnGroundProperly(spawned_car)
  SetModelAsNoLongerNeeded(vehicle)
end

Check this:

https://runtime.fivem.net/doc/reference.html#_0xA53ED5520C07654A

In Combination with this:

https://runtime.fivem.net/doc/reference.html#_0x7196842CB375CDB3

@ReynoldsCahoon: Does GetClosestVehicleNodeWithHeading only return position if there is no player car or no player there? I don’t find anything related to this in the documentation.

@Flatracer: Yup I was thinking about this but I would need to loop through all entities at this place (i don’t see how to do that yet) to disable collisions and then, loop to see if any of these is far enough from the car to re-enable collisions.
ie: if there is two players on bikes at the car spawn point.

It seems a bit overwhelming :confused:

It’s weird that I can’t find a native disabling collisions for the current entity and only against other entities (ped, vehicle, etc).

I tested it, and it doesn’t appear to avoid collision. I suppose you could make a loop that takes an offset from the point based on the heading and continues to find nodes, while only returning them when there’s nothing to collide with. Unfortunately I haven’t coded something like that yet.

I cant get the right spawn position i get always spawn at x=0, y=0 :confused: