Build a 2D Top Down Game – Zero to Published – Part 6

Welcome to Part 6 of the series.  We are really starting to cruise along here.  This part is all about instantiating prefabs on a timer.  So while your game is going, this will create new spiders on a certain time frame so we can have something to fight!

If you need to reference the previous articles, they can be found here:

Part 1:  Getting Started

Part 2: Animations

Part 3: Prefabs

Part 4: Enemies

Part 5: Modular Scripting

Part 6: Timed Prefab Instantiation

Part 7: Health Bars

Part 8: Finishing Touches

Part 9: Publishing

Create a Spider Web that Spawns Spiders!

  1. Locate the Spider Web Sprite in your sprites folder.  Drop this sprite onto the scene.
  2. Select the spider web and find the inspector.  Change the sorting layer for the Sprite Renderer Game Component in the inspector for the Spider Web to be set to “Obstacles”.  Again this is the sorting layer.
  3. Change the scale of the spider web to something you like.
  4. With the spider web game object still selected, Select “Add Component” -> Scripts -> New Script from the Inspector.  Name this script ‘TimedSpawner” and place it in the Scripts Folder.
  5. Add the required Variables
  6. //the object you will spawn.
    public GameObject objectToSpawn;
    
    //how long in seconds till you spawn. 
    public float timeToSpawn = 10.0f;
    
    //max number of spiders per web.
    public int maxObjects = 15;
    
    //keeps track of the internal timing.
    private float internalTimer = 0.0f;
    
  7. Create some code that spawns objects in your update function
  8. /// <summary>
    /// Updates this object every frame.
    /// </summary>
    private void Update () 
    {
    	this.internalTimer += Time.deltaTime;
    
    	if (this.internalTimer > this.timeToSpawn
    	    && this.spawnedObjects.Count < this.maxObjects)
    	{
    		this.Spawn();
    		this.internalTimer = 0.0f;
    	}
    }
    
  9. Write your code to spawn a single object.
  10. /// <summary>
    /// Spawns the defined object.
    /// </summary>
    privatevoidSpawn()
    {
    	//instantiates the defined object
    	//located directly on top of this object
    	//with zero rotation.
    	Instantiate(this.objectToSpawn,
    		this.transform.position,
    		Quaternion.identity);
    }
    
  1. Drop the prefab onto the object to spawn public property (you do this in the editor with the web selected)
  2. Create a new prefab named “SpiderWeb” in the prefabs folder and drop the spider web onto it.
  3. Delete the spider web and drop several around your scene.

 

5 thoughts on “Build a 2D Top Down Game – Zero to Published – Part 6

  1. Pingback: Build a 2D Top Down Game - Part 9 - Publishing - Dave Crook's Dev Blog - Site Home - MSDN Blogs

  2. Pingback: Build a 2D Top Down Game - Part 9 - Publishing - Dave Crook's Dev Blog - Site Home - MSDN Blogs

  3. Pingback: Build a 2D Top Down Game – Zero to Published – Part 9 | DaCrook

  4. Pingback: Build a 2D Top Down Game – Zero to Published – Part 2 | DaCrook

  5. Pingback: Build a 2D Top Down Game – Zero to Published – Part 5 | DaCrook

Leave a Reply

Your email address will not be published. Required fields are marked *