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 /
  • Help Room /
avatar image
0
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
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
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
Add comment · 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
0

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

Instead of using InvokeRepeating just use: Spawn();

Comment
Add comment · 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 ins$$anonymous$$d? 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

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

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

When I Build my game it cannot load a JSON file. 0 Answers

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

2D Weird Jumping 1 Answer

How do I get a bullet trail to follow a movile joystick? 0 Answers

Changing Color 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