Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. The Contract
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

C# Best way to spawn peds nearby randomly


Foxtrot64
 Share

Recommended Posts

I've tried searching for the best way to do this however I'm not entirely sure how to word it best.

 

I know we can use Game.Player.Character.Position.Around(distance) but this often spawns your ped under or above ground. Yes, the game does eventually pull them to the surface but I would like the Z axis to be determined as above ground before they are spawned there. Again, offsetting the player coordinates will can spawn them above/over ground and in walls.

 

I'm also working on a mod that has no world peds/vehicles so I can't really latch on those that the game has already made. I'm running a loop and will be spawning up to 50 peds in the vicinity of the player. They all need to be above ground and not stuck in a building etc.

 

Thanks

Link to comment
Share on other sites

I've tried searching for the best way to do this however I'm not entirely sure how to word it best.

 

I know we can use Game.Player.Character.Position.Around(distance) but this often spawns your ped under or above ground. Yes, the game does eventually pull them to the surface but I would like the Z axis to be determined as above ground before they are spawned there. Again, offsetting the player coordinates will can spawn them above/over ground and in walls.

 

I'm also working on a mod that has no world peds/vehicles so I can't really latch on those that the game has already made. I'm running a loop and will be spawning up to 50 peds in the vicinity of the player. They all need to be above ground and not stuck in a building etc.

 

Thanks

 

 

Can i just ask, what are you intending to do with "50" Peds? If you are intending to make them all do tasks, that is not going to go down well.

 

You can do something like

 

Ped player = Game.Player.Character;
public PedSpawner()
{
Tick += OnTick;
Interval = 10000;
}
void OnTick(object sender, EventArgs f)
{
World.CreateRandomPed(player.Position + (player.ForwardVector * 10) + (player.UpVector * 20));
}
}
Don't go to like 10; on the interval or peds will just start dropping from the air and probably crash your game..
But this is basically all you need. Change the (player.forwardvector) to a distance you like (test it out) then if the peds arent falling from high enough in the air (even though they'll probably die if you go much higher) then adjust that too.
Also, like I said, if you intend to make the peds do tasks, you will have to define the peds and you should probably only have 5-7 doing tasks at the same time or a lot of them may just stop responding and stand still.
If you want to make them spawn at different times, i think you can just make a randomizer and make a time period and let it pick a time between that period (not too sure about that)
if you want to make the peds location random, (or at least appear random around the players location) you can also make a list of positions around the player and then make a randomizer to pick out of one of those positions
Vector3 playerNorth = player.ForwardVector * 20;
Vector3 playerSouth = player.ForwardVector * -20;
Vector3 PlayerEast = player.RightVector * 20;
Vector3 PlayerWest = player.RightVector * -20;
then just make a radomizer to pick out of the list above. (im not too sure about this as i cant make a "List<Vector3>")
Just a pointer, i dont think this will have any guarantee that the peds wont spawn in a building, as we're telling them to spawn at Vector locations, regardless of buildings, but it should be close to a radius around the player, especailly if you do it like the compas (north, south, east, west), if you're making them run around buildings to hunt the player down, they spread out and make it look like they're coming from within the player radius and not just North, South, East or West, or if you wanted to go even farther, you could probably make cords for SouthWest, NorthEast, etc.
I doubt this is what you want, but this is all i can picture in my head :p
Edited by Colata
Link to comment
Share on other sites

Prof_Farnsworth

There are a couple of ways to go about this. One is to spawn them high in the air, then use "GetGroundZ" function to drop them to the ground, however, this will drop them into buildings on occasion still. I am sure you can also use "GET_NTH_VEHICLE_NODE" but I have no experience with that.

 

The way I do it is cheating. I spawn a car, then use "PlaceOnGround" and "PlaceOnNextStreet", create the ped in the car, then delete the car. This is the best way I have found to make sure they always spawn somewhere reliable.

 

Here is an excerpt from my assassination mod:

new Model("cavalcade").Request();while (!new Model("cavalcade").IsLoaded) Wait(10);Vehicle tempspawn = World.CreateVehicle("cavalcade", playerped.Position.Around(650f));if (Entity.Exists(tempspawn)){tempspawn.PlaceOnGround();tempspawn.PlaceOnNextStreet();targets[i] = tempspawn.CreatePedOnSeat(VehicleSeat.RightRear, pedTemp);if (Entity.Exists(targets[i])) { tempspawn.MarkAsNoLongerNeeded(); tempspawn.Delete(); }}
Link to comment
Share on other sites

I got a weird warning tell me that GetGroundZ was deprecated. GetGroundHeight worked fine. I can make a random spawn location with set distance from the player, then update it's Z axis to always spawn it above ground. This often means that they'll just spawn on top of buildings. I'm going to run a few tests and perhaps make the program come up with a new random spawn if the difference between player and GetGroundHeight Z coordinates are too different.

 

Likewise, if the spawn location so happens in water, they spawn at the bottom of the ocean/pool/lake etc. I could run a number checks to see if they are in water, and again spawn elsewhere. They also get pushed out of vehicles if they so happen to spawn inside them. I wish the game would do the same for buildings but whatever.

 

Here's some code:

Ped player = Game.Player.Character;Vector3 spawn = player.Position + (player.ForwardVector * 20);spawn.Z = World.GetGroundHeight(spawn);

Both of your suggestions are great. Thanks guys.

Link to comment
Share on other sites

Prof_Farnsworth

Right, GetGroundZ was updated, sorry. And water spawns is why I started to use the car to spawn them, now that you remind me, since it was easier than respawning and chancing water again. There was a native I found that was something like "IS_PED_SPAWN_RELIABLE". If I can find it I'll edit this, but I didn't ever get it working.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.