Sponsored: A tower defense-style game in Unity (part 2)

July 20, 2015
protect

Author: by Dimitris-Ilias Gkanatsios, Tech Evangelist, Microsoft Greece

This tutorial is brought to you by Microsoft.

If you’re in a hurry, you can find the complete source code on GitHub.

It is recommended that you read part 1 before proceeding on reading the rest of this tutorial, since it is heavily based on what we described in the first part! Don’t tell me I didn’t warn you!!!

This is the second post in two post tutorials regarding making a tower defense game in Unity. In the first post we described the game scenario and gameplay and we also mentioned the level editor, the XML file and structure that holds level information and the object pooler methodology we use. Again, if you haven’t read it, I strongly encourage you to do it  ASAP, this post will be much clearer after that!

We’ll start by exploring the prefabs we have in our game.

The RootGO gameobject

In the instructions we gave at the previous post about making a new level, we mentioned dragging a prefab called “RootGO” to the scene. Let’s see this object’s children in detail.

image

PathPieces

PathPieces is an empty game object that acts as a parent to the path sprites we will create, based on the details we’ll fetch from the XML file. If you’re wondering if it serves any other purpose other than the better organization of assets in the scene, then the answer is, pretty much, no.

Waypoints

Same as above, this object will hold waypoint game objects.

Background

The Background game object is a parent to the various sprites that make up our background.

image

ScriptHolder

This object has various script components, extremely useful to our game.

image

We can see references to a GameManager script, a DragDropBunny script, an AudioManager script and an ObjectPoolerManager script. If you’ve read the previous post, you’ll recognize the ObjectPoolerManager script that will assist us with the arrows and audio objects creation.

Bottom-BunnyGenerator

The Bottom game object holds a BunnyGenerator which is a simple bunny sprite and a simple black background, which will act as a background to the game details that will be visible to the user (current round, lives available etc.). The bunny sprite will be dragged along the screen to create new protector bunnies. The BunnyGenerator is also referenced from the DragDropBunny script and the bunny sprite is referenced in the GameManager (check above screenshot).

CarrotSpawner

The CarrotSpawner game object holds the CarrotSpawner script which has the duty to spawn carrots in the game scene, for our player to tap/click and gain carrot money.

GUIText

The GUIText game object holds a GUIText component which displays game related info (lives left, money left, current round etc.).

The color changing background sprite

Here, we’d like to highlight an important UI aspect of the game. When the user drags a new bunny onto the scene, there are some areas that it should not be placed, e.g. onto the paths. In order to make this visible to the user, we are making the background sprite appear red (we’re ‘tinting’ it).

image

image

By checking each bg_tile (the game object that carries the background sprite – children of the Background game object), we can see that it has a “ColorTint” material that has a “Sprites/ColorTint” shader. This is a custom shader that we created with the help of this post in reddit. We downloaded the official Unity shaders, opened the Sprites-Default.shader and modified the line

fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;

to write

fixed4 c = tex2D(_MainTex, IN.texcoord) + IN.color;

In this way, when we assign a color to the material that the shader is applied on, the pixel’s color will have the assigned color added and not multiplied (as in the default shader). In this way, when we give the material a Red color, the material will get “more red” while preserving the initial color values. We also changed the shader name (first line of the shader script) to “Sprites/ColorTint”.

image

Finally, we created the ColorTint material that contains this shader and we applied it to each background sprite.

Our prefabs

Before diving into the code, we’d like to briefly mention the prefabs to be used in the game.

Arrow

Let’s take a look at the Arrow prefab.

imageimage

The Arrow prefab is basically a game object with a RigidBody2D, a BoxCollider2D and an Arrow script. The SpriteRenderer is contained in a child game object and not in the parent one. You may wonder why this is happening. Answer is that the default arrow sprite points to the right, whereas we want the initial rotation for the arrow to be pointing to the top. So, we place the sprite game object into another game object and we assign a proper rotation to the sprite. We would follow the same strategy if we wanted to change the pivot point of a game object.

image

Things worth mentioning are also the Arrows Box collider (pictured above) and the fact that its rigidbody2D has a gravity scale of 0, so that it is not affected by gravity (it wouldn’t make sense for the arrow to fall down on the y-axis for this game). Finally, the Arrow is tagged as “Arrow”.

Bunny

imageimageimage

The Bunny prefab has a BoxCollider2D (to help us with its dragging – described later), follows the same strategy as the Arrow for its rotation (SpriteRenderer is contained in a child game object) and has the Bunny script which references the ArrowSpawnPosition, which is the location where the arrows will be shot from (pictured in the blue shape above).

Carrot

image

The Carrot prefab is a simple one, having a sprite renderer, a BoxCollider2D component (to recognize user’s taps/clicks) and a Carrot script.

Enemy

imageimageimage

The enemy prefab follows the same strategy for its rotation like Arrow and Bunny. It also has a PolygonCollider2D component (boundaries illustrated in the above image) and the Enemy script.

Path

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>