- Home /
spawning gameObjects into different formations
I was wondering how I might go about spawning gameObjects in different pre-specified formations. I am trying to spawn 5 gameObjects into a random formation: i) 5 objects aligned horizontally across top of screen, OR ii) horizontally aligned across middle of screen, OR iii) horizontally aligned across bottom of screen, OR iv) V formation pointing upwards, OR v) V formation pointing downwards (example attached). Then, after a button press, these gameObjects spawn into another formation (chosen randomly). Would I start by creating an array of formations somehow? And how might I go about spawning my gameObjects into these formations? Any help would be greatly appreciated!
Answer by Kennai · Jul 30, 2019 at 10:06 AM
Hello, @lalalanni !
The easiest way is:
Create empty game object and name it "Formation 1"
add 5 childs empty game objects
Place childs as one of your possible formation - in a line or in V-formation
go to p.1 and create another formation
Once you've created all possible formations, you will have a list of game objects "Formation 1", "Formation 2" and so on.
Create a new script with a parameter List of GameObject, in Inspector view assign all your formations to this list
Now you are ready to spawn your game objects by randomly choose index of a list, get child's positions and copy this positions to your 5 game objects.