I am trying to spawn ped in a “safe way”, I mean trying to make them spawning in a safe place and on the ground directly. For that I am using a trick: create a car with a ped and then delete the car.
This avoid the ped to spawn far in the sky or above sea or building and so on…
But the code make the game crash! I don’t know why. If someone can help.
Here is the code:
public async void SpawnPed()
Random rndSpawn = new Random();
while (!new Model("cavalcade").IsLoaded)
Vector3 playerPos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, Game.Player.Character);
float newPosX = playerPos.X + rndSpawn.Next(-10, 10);
float newPosY = playerPos.Y + rndSpawn.Next(-10, 10);
carSpawned = Function.Call<Vehicle>(Hash.CREATE_VEHICLE, "cavalcade", newPosX, newPosY, playerPos.Z, 1f, true, false);
while (!Function.Call<bool>(Hash.HAS_MODEL_LOADED, "S_M_Y_Construct_02"))
Ped pedToSpawn = Function.Call<Ped>(Hash.CREATE_PED_INSIDE_VEHICLE, carSpawned , 4, "S_M_Y_Construct_02", -1, true, false);
Function.Call(Hash.SET_ENTITY_AS_MISSION_ENTITY, carSpawned, true, true);