Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by K_Tec · Jul 04, 2017 at 06:56 PM · scripting problemarrayaispawning

(No anwers? :( )How to spawn Random Pedestrians in a raduis?

Hello Everybody,

im working on a Open-World game with a big, big ,big Open World. I have some Pedestrians with my AI (Nav Mesh, Active Ragdoll based ) and i want that they spawn in an area 100 meters before the player, and 100 meters after the player they de-spawn. Like A circle- but they only should spawn were place is, not inside a tree or a car :D. Also i could use a pool for the Humans. How would i do the spawning and the pool?

thanks in advance!!!

Regards, T

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by efeguclu · Jul 04, 2017 at 07:37 PM

Check out Instantiate and Random.Range from documentary

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image K_Tec · Jul 04, 2017 at 07:38 PM 0
Share

ok i will try it out thx

avatar image Vollmondum · Jul 04, 2017 at 07:55 PM 0
Share

How would Random.Range help him not to instantiate peds inside trees or pools? :))

avatar image
0

Answer by Cornelis-de-Jager · Jul 04, 2017 at 10:38 PM

Hi,

This question can be broken up into different steps. All within a modified for loop:

 for (int i = 0; i < NumberOfSpawns; i = i) { /* Code Below Goes In Here*/ }

Note that we are not doing i++. Because we want to manually increase the count if we had a successful spawn.

Within the for loop we need a few steps:

Step 1: The first is to find a random spawning position.

This can be done with the Random.Range() function mentioned above. Use the following Code:

 // Get position of Circle
     float circleParam = 2 * Mathf.PI * Random.Range (0.0f, 1.0f );
     // Select Start Position
     Vector3 startPos = new Vector3 (
                      radius * Mathf.Sin (circleParam),
                      0.5f, /* Spawn above ground */
                      radius * Mathf.Cos (circleParam )
      );
 

Step 2: Check for Collision

In this step use SpherecastAll to check for collision. below is a written function that checks for collision within a certain radius.

 // Inside for loop:
 bool canSpawn = !isCollision (prefabRadius, startPos);
 
 
 // The function
 bool isCollision (radius, position) {
       // Create new ray
       Ray ray = new Ray(position, Vector3.up);
 
      // Create Raycast hits array
       RaycastHit[] hits;
        
       // Do the raycast
       hits = Physics.SphereCastAll(ray, radius, 0f); //Can change 0f to prefabHeight / 2.1f if you need to check height
 
 return hits.length > 0;
 }

Step 3: Spawn or continue to next step

No that we know if it will collide with an object or not we can write the end of the for loop here:

 if (canSpawn) { // you can spawn it so do it
     // Instantiate the object
    GameObject obj = Instantiate (prefab, startPosition, Quaternion.identity * Quaternion.Euler (new Vector3(0, Range.Random(0, 359), 0))) as GameObject;
 
   // IMPORTANT: Increase the loop counter or you will get stuck in the loop
 i++;
 } else 
     continue; // Step to the next iteration without increasing i

All of this will ensure that you spawn the selected amount of objects and that they do not collide.

FINALLY: If you want them to despawn when out of range then keep track off all the objects in a list and use this function below in the LateUpdate function:

 void LateUpdate () {
    for (int i = 0; i < enemyList.Count; i = i) {
   // Get the distance between them
    float distanceFromPlayer = Vector3.Distance (playerPosition, enemyList[i].transform.position);
   
    // Remove if greater than radius
    if (distanceFromPlayer > radius) {
       // remove from list
       enemyList.Remove (enemyList[i]); 
    } else {
    // No Object Removed so increase i 
   i++;
   }
   }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image K_Tec · Jul 05, 2017 at 12:40 PM 0
Share

Thank you man!!!! For some reason there is no accept button on this answer :(

Thanks you!!!

Regards, T

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

More Efficient way? 1 Answer

Object Pool only activating one prefab 1 Answer

JS: Access sections of an array, without activating the rest 2 Answers

How can I use a variable as length of an array? 1 Answer

Can't properly duplicate an enemy AI 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges