- Home /
How can I go about creating an "Infinate" asteroid field??
HI!
Ok so what i have, is a top down game, with a little ship you can fly around.
What I want, is to be able to fly for any distance in any direction, and still run into asteroids. Obviously I cant instantiate hundreds of asteroids and never delete them - so what Im after is any advice or techniques I could use to create this effect? What I had thought might work is something like a spawner/catcher system, that is parented to the ship. So it spawns asteroids off screen, around the player, and then when you move past them, the catcher deletes them, off screen aswell... Im not sure if this is a convoluted idea though - perhaps there is a better way?
Thanks in advance!
Answer by SarperS · Jun 22, 2016 at 05:46 AM
What you require is an object pooling system. Basically you will only instantiate the desired amount of objects in the start of the scene and put them in a container (array, generic list etc.)
Then you have to enable and position those objects (only use the non active ones) based on the player's velocity (where the player is heading) just outside the viewport. And also disable the ones which leave the viewport and not in the direction the player is heading.
Do a google search for Unity3d object pooling. Here is an official tutorial https://unity3d.com/learn/tutorials/topics/scripting/object-pooling?playlist=17117
Thanks very much :D I shall look into this - hopefully its not too complex, as i am an artist, not a programmer haha
Well, learning is fun! And if you don't want to spend too much time on it I'm sure there is something for this in the Asset Store :)
Your answer
Follow this Question
Related Questions
Random generation of Asteroids 3 Answers
Create with Code 4.4 For-Loops For Waves: play mode freeze 0 Answers
GUIText Instantiation to show at Asteroid position 0 Answers
Instantiate a prefab that has rigidbody cause infinite clones 1 Answer
why does Infinite Runner Terrain Collider break my game? 0 Answers