Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by jackkoonie · Nov 29, 2020 at 03:07 PM · coroutinesrangespawning-enemies

Spawner within range

I'm looking for a way to have a spawner instantiate enemies when the player is in range, and stop spawning when the player leaves that range. I thought a Coroutine would work, but I'm pretty new at it and with a Coroutine it's just spawning from the beginning. I also haven't a single clue how I would get it to stop once the player gets out of range. I've tried replacing "while (true)" with "while(targetDistance <= spawnRange)" and removing the if statement from my Start, but no dice.

     GameObject enemyToSpawn;
     Transform enemyParent;
     float spawnTime = 2f;
     Transform target;
 
     float targetDistance;
     float spawnRange = 10f;
 
     void Start()
     {
         if (targetDistance <= spawnRange)
         {
             StartCoroutine(SpawnEnemies());
         }
     }
 
     void Update()
     {
         targetDistance = Vector3.Distance(target.transform.position, gameObject.transform.position);
     }
 
     IEnumerator SpawnEnemies()
     {
         while (true)
         {
             var enemyWave = Instantiate(enemyToSpawn, transform.position, Quaternion.identity);
             enemyWave.transform.parent = enemyParent;
             yield return new WaitForSeconds(spawnTime);
         }
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Nov 29, 2020 at 03:41 PM

 GameObject enemyToSpawn;
 Transform enemyParent;
 float spawnTime = 2f;
 Transform target;
 
 float targetDistance;
 float spawnRange = 10f;
 float nextSpawnTime = 0;
 
 void Update()
 {
     targetDistance = Vector3.Distance(target.transform.position, gameObject.transform.position);
     if (targetDistance <= spawnRange && Time.time > nextSpawnTime)
     {
         SpawnEnemy();
     }
 }
 
 void SpawnEnemy()
 {
     var enemyWave = Instantiate(enemyToSpawn, transform.position, Quaternion.identity);
     enemyWave.transform.parent = enemyParent;
     nextSpawnTime = Time.time + spawnTime;
 }
Comment
Add comment · Show 6 · 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 jackkoonie · Nov 29, 2020 at 03:51 PM 0
Share

Almost perfect, and very quick response. Only issue is that when I enter the spawner's range it sends a burst of 3-5 enemies before it starts following my "spawnTime" float

avatar image Hellium jackkoonie · Nov 29, 2020 at 03:54 PM 0
Share

You may have attached the script multiple times then.

In SpawnEnemy, add Debug.Log(gameObject.name + " has spawned enemy", gameObject); and click on the messages to see which gameObject spawned the enemy

avatar image jackkoonie Hellium · Nov 29, 2020 at 04:10 PM 0
Share

Only one copy of the script on the spawner, is it possible that by entering range there's a moment where I'm half in range and it's causing it to double process? alt text

ttt.png (8.2 kB)
Show more comments
avatar image Hellium jackkoonie · Nov 29, 2020 at 05:22 PM 0
Share

$$anonymous$$y bad, I fixed the code. Only the last line of SpawnEnemy has changed.

avatar image jackkoonie Hellium · Nov 29, 2020 at 05:36 PM 0
Share

Perfect. Works like a charm. $$anonymous$$uch appreciated!

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

212 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 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

How to provide a chraracter to dissamble the parts of a bridge by range while crossing? 0 Answers

How do I make an array with numbers between to values? 0 Answers

Making a growing number 1 Answer

use Update instead of Coroutines 1 Answer

Decision making coroutine via input 1 Answer


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