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 /
This question was closed Jun 04, 2018 at 03:50 AM by markcagatandavis for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by markcagatandavis · May 27, 2018 at 01:31 PM · spawningspawnpointsradius

Spawn Object Outside of Radius

Hey Guys,

I was wondering how would I spawn an enemy object outside of a player objects radius?

For example, I have enemy ship objects that spawn at random positions onto the play field. What I would like to do, is create a radius (let's say of 5) around the player so when a ship spawns, it doesn't spawn on top or next to the player. This also gives a chance for the player to react to an enemy spawn.

I'm not sure how I would go about this, so I don't have code for it. At the moment this is the code for my spawner:

 IEnumerator SpawnWaves()
 {
         yield return new WaitForSeconds(startWait);
         while (true) {
             for (int i = 0; i < hazrdCount; i++)
             {
                 GameObject hazard = hazards[Random.Range (0, hazards.Length)] ;
                 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate(hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds(spawnWait);
             }
             yield return new WaitForSeconds(waveWait);        
         }
     }

Right now the enemy objects don't spawn outside of the world, which is good. But I would like to make sure no matter where the player object is on the map, enemy objects won't spawn relative to the player object of a given radius.

Thanks, Mark.D

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

3 Replies

  • Sort: 
avatar image
4

Answer by jim3878 · May 27, 2018 at 02:49 PM

This will make sure enemy ship will spawn at a distance between 5.0 to 20.0.

  float minDistance = 5.0;
  float maxDistance = 20.0;
  float distance    = Random.Range( minDistance, maxDistance );
  float angle       = Random.Range( -Mathf.PI, Mathf.PI );
  
  Vector3 spawnPosition = playerTransform.position ;
  spawnPosition += new Vector3( Mathf.Cos( angle ), 0, Mathf.Sin( angle ) ) * distance;

  spawnPosition.x = Mathf.Clamp( spawnPosition.x, -spawnValues.x, spawnValues.x );
  spawnPosition.y = spawnValues.y ;
  spawnPosition.z = Mathf.Clamp( spawnPosition.z, -spawnValues.z, spawnValues.z );


Comment
Add comment · Show 19 · 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 markcagatandavis · May 28, 2018 at 06:33 AM 0
Share

Hi Jim,

I believe I have this some what working now, I can get the enemies to spawn in a random location from $$anonymous$$Distance * maxDistance, but this doesn't seem to make it relative to the player position. It is only relative to centre position. Am I missing something?

     IEnumerator SpawnWaves()
     {
         yield return new WaitForSeconds(startWait);
         while (true) {
             for (int i = 0; i < hazrdCount; i++)
             {
                 float $$anonymous$$Distance = 30f;
                 float maxDistance = 35.0f;
                 float distance = Random.Range($$anonymous$$Distance, maxDistance);
                 float angle = Random.Range(-$$anonymous$$athf.PI, $$anonymous$$athf.PI);
 
                 GameObject hazard = hazards[Random.Range(0, hazards.Length)];
                 Vector3 spawnPosition = hazard.transform.position;
                 
                 spawnPosition += new Vector3($$anonymous$$athf.Cos(angle), 0, $$anonymous$$athf.Sin(angle)) * distance;
                 spawnPosition.x = $$anonymous$$athf.Clamp(spawnPosition.x, -spawnValues.x, spawnValues.x);
                 spawnPosition.y = spawnValues.y;
                 spawnPosition.z = $$anonymous$$athf.Clamp(spawnPosition.z, -spawnValues.z, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate(hazard, spawnPosition, spawnRotation);
 
 
                 yield return new WaitForSeconds(spawnWait);
             }
             yield return new WaitForSeconds(waveWait);
 
             if (gameOver)
             {
                 restartText.text = "Press 'R' for Restart";
                 restart = true;
                 break;
             }
         }
     }
avatar image Unified2000 markcagatandavis · May 28, 2018 at 07:05 AM 0
Share

Vector3 spawnPosition = player.transform.position;

or if you have the main camera attached to the player then you could do this:

Vector3 spawnPosition=Camera.main.transform.position

avatar image markcagatandavis Unified2000 · May 29, 2018 at 07:01 AM 0
Share

Hmmm I tried that, but it doesn't seem to work. I added a DrawOnGizmos() to the spawn position that way I can see the spawn radius and it seems to just go in random locations all the time... I'll create a snippet of what it's doing so you can see :) Thanks for your wonderful help :)

Show more comments
avatar image Bunny83 · Jun 04, 2018 at 09:53 AM 0
Share

This has several issues. First of all the point you pick on the disk is not uniformly distributed. You get a higher density at the inner circle. Also your clamping increases the density in the corners of your clamping region.


A common way to fix the center concentration is to take the square root of the radius. However this only works for picking a point inside a complete circle. We could adjust the formula to get a uniform distribution at least inside the annulus. Though the clamping would still cause a higher density towards the corners.


If the spawning area can be a circle in the first place it would be possible to get a uniform distribution inside a ring / annulus. If the spawning area should be a rectangle then @tormentoarmagedoom's answer is the only one that guarantees a uniform distribution.

avatar image
0

Answer by tormentoarmagedoom · May 27, 2018 at 01:56 PM

Good day.

As you randomize the spawn point, you can just check the distance from the spawnpoint to the player, and if is too close, recalculate the spawnpoint, like this:

 GameObject hazard = hazards[Random.Range (0, hazards.Length)] ;
 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
 while (Vector3.distance(spawnPosition, player.transform.position) < 50)
 {
 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
 }
 Quaternion spawnRotation = Quaternion.identity;
 Instantiate(hazard, spawnPosition, spawnRotation);

Now, it will be doing the While sentence remaking the spawnpoint position until the distance is more than 50.

Be aware to check the while condition before play the game in Editor, because if while never become false, it willstay isidie the while "forever" and Unity will stay blocked, you you will need to Ctl+Alt+Sup to stop Unity ... so save the project every time until you are sure the while works perfect.

Bye! :D

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 Hellium · May 27, 2018 at 03:02 PM 0
Share

$$anonymous$$gest a solution that may take an unknown duration to calculate, and worse yet, that can crash the built game or the editor is not very smart.

avatar image Bunny83 · Jun 04, 2018 at 09:38 AM 0
Share

Upvoted since it actually provides a uniform distribution. Yes, it's not an optimal solution and with the wrong setting could lead to an infinite loop. However it is actually the only solution to pick a uniformly distributed random point inside a rectangle and exclude a spherical region in the center. If the ratio between the spawnValues and the inner radius is getting small (or even below 1f) the performance would drastically drop (or even hang your application).

avatar image
0

Answer by Unified2000 · May 27, 2018 at 02:30 PM

This will return a random position that's 20 to 50 meters away:

  Vector3 pos=new Vector3(Random.value-0.5f,0,Random.value-0.5f).normalized*Random.Range(20,50);

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 tormentoarmagedoom · May 27, 2018 at 02:43 PM 0
Share

But this does not control if is inside the world map...

avatar image Bunny83 · Jun 04, 2018 at 09:27 AM 1
Share

This work but has a non-uniform distribution. The density on the inner circle is higher than on the outer circle.

Follow this Question

Answers Answers and Comments

89 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

Related Questions

ai spawning please help 0 Answers

Random spawnpoint where only one prefab can spawn! 1 Answer

How to spawn objects using an array of spawn points w/o intersecting 1 Answer

How to spawn enemies on my spawnpoints 1 Answer

Spawn prefabs in a circle? 3 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