• 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
Question by Ashton-Benedict · May 21, 2016 at 09:59 PM · c#2d-platformerenemy spawn

Enemy Spawning Help (2D Platformer, C#)

Hi, I am going to have multiple spawn points with this script attached. And I want the enemies to spawn after a certain amount of time, but only when the player is in a certain range ("maxDistance"). And not spawn enemies at all when the player is not in that range. This part works, the problem is the when I start the game and after the ("spawnDelay"), It spawns enemies rapidly and doesn't stop. It ignores the ("spawnTime"). Please help me with this.

Script (C#)

 public class Spawner : MonoBehaviour {
 
     public float spawnTime;        // The amount of time between each spawn.
     public float spawnDelay;       // The amount of time before spawning starts.
     public GameObject[] enemies;        // Array of enemy prefabs.
 
     public int maxDistance;
     public Transform target;
     public Transform myTransform;
 
     void Awake()
     {
         myTransform = transform;
     }
 
     void Start()
     {
         GameObject stop = GameObject.FindGameObjectWithTag("Player");
 
         target = stop.transform;
 
         maxDistance = 7;
     }
 
     void FixedUpdate()
     {
         if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
         {
             InvokeRepeating("Spawn", spawnDelay, spawnTime);
         }
     }
 
     void Spawn()
     {
         // Instantiate a random enemy.
         int ZombieIndex = Random.Range(0, enemies.Length);
         Instantiate(enemies[ZombieIndex], transform.position, transform.rotation);   
     }
 }

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Ashton-Benedict · May 23, 2016 at 03:59 AM

Found the perfect solution!!! Here is the script if anyone needs it:

 public class Spawner : MonoBehaviour {
 
     public float spawnTime;        // The amount of time between each spawn.
     public float spawnDelay;       // The amount of time before spawning starts.
     public GameObject enemy;
 
     public int maxDistance;
     public Transform target;
     public Transform myTransform;
 
     void Awake()
     {
         myTransform = transform;
 
     }
 
     void Start()
     {
         GameObject stop = GameObject.FindGameObjectWithTag("Player");
 
         target = stop.transform;
 
         maxDistance = 5;
 
         StartCoroutine(SpawnTimeDelay());
     }
 
     IEnumerator SpawnTimeDelay()
     {
         while (true)
             {
                 if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
                 {
                 Instantiate(enemy, transform.position, Quaternion.identity);
                 yield return new WaitForSeconds(spawnTime);
                 }
 
                 if (Vector3.Distance(target.position, myTransform.position) > maxDistance)
                 {
                     yield return null;
                 }                
             }
     }
 }

I had to use a while loop with the WaitForSeconds statement. Thanks for no ones help :P It took me for ever!

Comment
aule

People who like this

1 Show 0 · 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

Answer by LLIV · May 21, 2016 at 11:29 PM

Instead of using InvokeRepeating just use: Spawn();

Comment

People who like this

0 Show 3 · 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 Ashton-Benedict · May 22, 2016 at 01:32 AM 0
Share

It didn't work, its still spawning enemies rapidly. I replaced

 InvokeRepeating("Spawn", spawnDelay, spawnTime);

with

 Spawn();

Is that what you meant? because it didn't fix it. :/

avatar image Ashton-Benedict · May 22, 2016 at 09:07 PM 0
Share

I tried doing something like this. Spawning the enemy if in range and waiting 7 seconds to spawn again, but it didn't work, maybe something like this instead? Please help.

 void FixedUpdate()
     {
         SpawnTimeDelay();
     }
 
     IEnumerator SpawnTimeDelay()
     {
         if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
         {
             Spawn();
             yield return new WaitForSeconds(7);
         }
         else
         {
             yield return null;
         }
     }
 
     void Spawn()
     {
         // Instantiate a random enemy.
         int ZombieIndex = Random.Range(0, enemies.Length);
         Instantiate(enemies[ZombieIndex], transform.position, transform.rotation);
     }
 }

@LLIV

avatar image wargreyguy25 · Nov 11, 2016 at 04:30 PM 0
Share

How do i apply the script in green above because even if i apply it because I've been using this tutorial for a project I'm making and have been looking for an enemy spawner but haven't been the right thing i need

been using this tutorial: https://unity3d.com/learn/tutorials/topics/2d-game-creation/creating-basic-platformer-game

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

158 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

Related Questions

Unintelligible errors in the console 1 Answer

Help with enemy AI Please! (on 2D Platformer) 0 Answers

Problem With a Multi-Jump C# script, robot 2d character controller 1 Answer

How can make it so only 1 object of type “Ability” can be selected at once? 0 Answers

Scrolling background wall collision problem 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