Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How should I go about spawning an enemy layout?

Discussion in 'Scripting' started by ardizzle, Sep 18, 2014.

  1. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    I am working on a game where you walk around the map in a 2D top down fashion. When you run into an enemy it changes camera over to a battle area for you to fight in. I have everything set up with that except for getting the enemy's to spawn in a specified layout. The amount of enemy's vary from 10 to about 45.

    As far as routes that I have thought of to solve this issues I know I can make an array to hold every prefab I want to spawn and their location but that would put a lot of work on the level designer since he would have to do that for every enemy unless we wanted every enemy to spawn the same layout. My other idea was to have it spawn a scene with all enemy's in place but that becomes very in flexible and with 10 -15 different layouts per map we would get really over crowed with scenes for doing so little. My last idea was to make all the enemy game objects the child of one empty gameObject and then save it as a prefab. Then when I needed that layout I would just spawn that prefab. But then I run into having many duplicates of already existing prefabs.

    So what do you think would be the best solution to this problem? Would It be one of my three ideas above or do you guys have a completely different solution for me? Thanks for the help :)
     
  2. rkaetano

    rkaetano

    Joined:
    Aug 27, 2012
    Posts:
    61
    Why not use some empty GO to define a area, like a square or a triangle ?
    Then, in your spawn script, you spawn all enemies inside this area.
     
  3. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    The problem with that is that all my enemies have specific locations they have to be in the make the level. I can't just have them spawn random. I could hard code every layout but that would take a long long time also.
     
  4. rkaetano

    rkaetano

    Joined:
    Aug 27, 2012
    Posts:
    61
    What you mean with "each enemy have a specific location" ?
    Can you give a example ?
    They will stopped or something like this ?
    Can you post a draw or screenshot to exemplify ?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,841
    I would define the layout as a GameObject containing a series of, say, cubes. Each cube represents the position where one enemy will spawn. You could even attach some sort of SpawnEnemy script to each cube that defines which sort of enemy to spawn, and any other parameters you want to configure (initial hit points, whatever). This script, on Start, will instantiate the indicated enemy prefab, set it up, and then destroy itself. So the player will never see the cubes, they'll only see the enemies.

    This makes it easy for the designer to make enemy layouts, and each layout can be saved in the project as a prefab so it's easy to create and instantiate one at runtime, use the same layout in multiple locations, etc.
     
    Zaladur likes this.
  6. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Well i just got finished typing a longer version suggesting exactly what JoeStrout suggested. Luckily Unity's new forum updates while I am inside the topic typing, so I didn't post a giant, redundant post.

    ^^^ Do what he said.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    To elaborate on JoeStrouts comment, we use a system where we have a level prefab with all the geometry and collision data, but no spawn locations.

    Then we have a totally different prefab with the enemy spawns for level 1, such that when loaded alongside the geometry, the spawn objects (which are invisible bare game objects with little colored pill editor tags on them) line up exactly with the geometry.

    Then we save that prefab separately. For level2 we have a different load pattern, etc.

    That way you can have (say) only level 10 has the boss spawn point.

    If you use simple names on the game objects (say 'enemy1' or 'enemyboss'), then your code is easy to debug, the code that actually recursively travels your enemy spawn prefab and looks for places to stick enemies.

    You can out player spawns in similarly. This also lets you orient the objects as well as positioning them, making the enemies face a particular direction, etc: you just copy the rotation of the spawn object to the newly-instantiated enemy.

    Kurt
     
  8. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Thats for the feed back guys :) after playing around with it a bit I ended up using somewhat of what Joe said. But rather than just using cubes as layouts I was able to set it up where we could just set the layout up using the enemy's. And then put them all as children under an empty game object. Then I set up which enemy will spawn which layout. When you hit them it sends the player to the battle area and enables the parent object. Then the parent object sets the level of all the enemy's and resets there health to there default.

    This way I can use the same layout multiple times. Don't have to spawn and destroy prefabs over and over. And its pretty easy for my level designer to use. Thanks for the feed back guys :)
     
    GarthSmith likes this.