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 /
avatar image
1
Question by pokobros · Feb 20, 2014 at 02:49 AM · prefabenemyinfiniteobject poolspawning-enemies

Enemy Spawning in Infinite Runner

I am trying to develop a system of randomly spawning enemies in an infinite runner. So, here is the process in which I am going to spawn the enemies. I am going to choose a time interval at which the enemy is spawned at based on difficulty. (time between each enemy spawning gets shorter as the infinite runner game goes on. Then I am going to randomly choose the distance apart the enemy will spawned at based on difficulty. Then choose from 4 different enemy choices. What I just explained is my grand, master plan, but I am still trying to just spawn one enemy let alone all 4. I have been reading all about prefabs, instantiation, object pooling, and reusing objects. What I have learned is that instantiation and destroying objects can be expensive, but so can object pooling at times. I am 13 years old and I have just begun developing in Unity so object pooling from what I can see is a pretty complex topic. You are probably more experienced than me so, what do you think is the best path to go down for what I am trying to do? This is not a write-me-the code question, it is just asking what path I should follow to spawn the enemies. Here is the code I have so far which generates enemies but gets to a point where the game gets so slow, it is unplayable.

 public Transform garbagePile;
 private float randomeTimeInterval;
 private float randomXDistance;
 private float lastXValue;
 private int randomZPos;
 private float[] randomZValues;
 private float theZValue;
 private int numOfGarbage;
 // Use this for initialization
 void Start() {
     CreateEnemy ();

     randomZValues = new float[3];
     randomZValues [0] = 0.0f;
     randomZValues [1] = -2.0f;
     randomZValues [2] = 2.0f;
 }

 void CreateEnemy () {
     randomXDistance = Random.Range (6, 9);

     randomZPos = Random.Range (0, 3);        
     theZValue = randomZValues [randomZPos];

     Vector3 pos = new Vector3((float)lastXValue + randomXDistance,0,(float)theZValue);
     Instantiate(garbagePile, pos, Quaternion.identity);
     lastXValue = garbagePile.transform.position.x;
 }
 // Update is called once per frame
 void Update () {
     Vector3 updatePos = new Vector3 ((float)0.2, 0, 0);
     garbagePile.transform.position -= updatePos;

     if(garbagePile.transform.position.x <= (float)-11){
         Destroy(gameObject);
         CreateEnemy();
     }
 }

}

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 dorpeleg · Feb 20, 2014 at 10:24 AM

You should use pools.

The complexity of pools depends on your game, for now its seems like this should be an easy pool system.

Here the basic of pooling system:

Decide on a maximum objects you want the pool to have.

When you want to create an object, first, check if he is in the pool.

If he is in the pool take him from there, if not, instantiate him and add to pool.

When you want to destroy an object, you simply disable him but keep him in the pool.

So actually, each time you try to create a new object, you need to test the following:

Is there a copy of the object I'm trying to create already in pool? if not, instantiate and add to pool.

If yes, is that object currently disabled? if not, instantiate and add to pool.

If yes, enable the object and move it to where you want (no instantiation).

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

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

19 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

Related Questions

bullets kill all enemies, not just the ones hit. 1 Answer

Destroy and Spawn an Enemy 1 Answer

making infinite road with prefabs 1 Answer

In Unity how can I set a value to property while initializing a prefab with Instantiate() method like OOP constructor with parameters? 1 Answer

enemy ai going to player 2 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