In a tower defense, enemies walk along a path and you build towers that shoot them before they reach your base. Here the enemies follow a path you draw yourself, each tower picks the closest enemy and fires at it, and credits pay for new towers.
- an Image named map: the background of the game, 1920 x 1080 px, with a road drawn on it;
- a few Images for the build spots (for example 120 x 120 px);
- an Image for the enemy (for example 64 x 64 px), one for the tower (120 x 120 px), one for the cannon (90 x 34 px, pointing right) and one for the bullet (24 x 24 px);
- two Text objects: creditsText and lifeText, at the top of the screen.
Step 1: the variables
- In the left panel, open the Variables section and create two variables, both with scope Global (all the scenes can use them) and type Integer:
- credits: starting value 150 (the money to build towers);
- base_life: starting value 20 (how many enemies your base can take).
Step 2: draw the path
The enemies follow a path: points linked together. You draw it on top of your map with the Path tool.
- Open the Assets window (Resources panel) and touch Path. The path editor opens, with your scene in the background.
- Touch New path and name it Path.
- With the tool Place points, touch the scene to place a first point on the left, where the enemies come in: it is the Start. Keep touching along your road: each new point is linked to the previous one.
- Put the last point on your base: it has no exit, it is the End.
- Touch Watch it go to see a dot travel along the path. Then close the editor: the path is saved by itself.
Step 3: the Enemy scene
An enemy is a scene of its own: every enemy you send into the game is a copy of it, with its own life. You build it once.
- Create a new scene named Enemy (menu +, then Create a scene).
- In the scene Enemy, add an Image named enemy at X =
0, Y =0, and give it the tag enemy. - Open the Blueprint of the scene Enemy and add the “On Start” event.
- Add “Give health to the object”: target enemy, Health points =
3(the enemy takes 3 hits). - Add “Follow the path”: target enemy, Path name = Path, Speed (pixels per second) =
150. - Link the three nodes in order.
Step 4: send the waves
Now go back to your main scene (Game) and send enemies one after the other.
- Open the Blueprint of the scene Game and add the “On Start” event.
- Add two “Modify Variable” nodes: credits =
150, then base_life =20. They start every new game clean, even after a Game Over. - Add “Wait”: Duration (seconds) =
3(a pause before the first enemy). - Add “Repeat”: Every (seconds) =
1.5, Number of times (0 = endless) =15, Timer name (optional) =waves. - From the Each time output, add “Place a copy of a scene”: scene Enemy. Leave X and Y as they are: the enemy goes to the start of the path by itself.
- Link the nodes in order.
Run the game: an enemy appears every second and a half and walks along your path to the end (nothing happens when it arrives yet).
Step 5: the Tower scene
A tower is also a scene. It looks for the closest enemy in range, turns its cannon toward it and fires.
- Create a new scene named Tower. Add an Image tower at X =
0, Y =0, then an Image cannon at X =15, Y =43, in front of it. - In the scene Game, add an Image named bullet outside the screen (for example X =
-300, Y =-300) and give it the tag bullet: it is the model the tower copies at each shot. - Open the Blueprint of the scene Tower and add the “On Start” event, then “Repeat”: Every (seconds) =
0.2, Number of times (0 = endless) =0(endless). The tower looks around 5 times per second. - From the Each time output, add “Choose a target”: target tower, Tags of possible targets (comma-separated) =
enemy, Which target to pick? = “The closest”, and in the advanced settings Range in pixels, as a circle (0 = use the range in cells) =330. - From the Target found output, add “Face towards”: target cannon, and as the second object choose The object involved in the event (the enemy that was chosen).
- Add “Cooldown”: target cannon, Delay (seconds) =
1.2(one shot every 1.2 seconds at most). - From the Ready output, add “Shoot an object”: target cannon, Model name =
bullet, Speed =14, Shot lifetime (seconds, 0 = unlimited) =1.6, Destroy off screen (yes / no) = no. Leave Direction as it is: the bullet leaves in the direction the cannon is facing.
Step 6: hit the enemies
This part goes in the scene Enemy: it is the enemy that knows when a bullet touches it.
- Open the Blueprint of the scene Enemy and add the “If object touches Tag” event: target enemy, Tag bullet.
- Add “Destroy Object” and choose The object involved in the event as the target: it is the bullet, which disappears.
- Add “Deal damage”: target enemy, Damage =
1. By default the enemy is destroyed when its life reaches zero. - From the Out of health output, add “Add to Variable”: variable credits, value
15(the reward). - Link the nodes in order.
Step 7: build towers
The player builds a tower by touching a build spot. It costs 50 credits.
- Select each build spot and give it the tag spot in the Inspector.
- In the scene Game, add the “On Object Click” event and choose # spot as the object (every object with the tag spot).
- Add “Condition”
=credits >= 50. - On the True output: “Add to Variable”: variable credits, value
-50. - Then “Place a copy of a scene”: scene Tower. For X, touch 🔗, choose “Object value”, object The object involved in the event, What =
x. Do the same for Y withy: the tower appears exactly where the player touched. - Add “Destroy Object” with The object involved in the event as the target: the build spot disappears.
- On the False output: “Small message at the bottom of the screen” with the message Not enough credits.
Step 8: the numbers on screen
- In the scene Game, add the “Every frame” event.
- Add “Modify Text”: target creditsText, New text =
="Credits: " + credits. - Add a second “Modify Text”: target lifeText, New text =
="Base: " + base_life. - Link the three nodes in order.
Step 9: base lives and game over
When an enemy reaches the end of the path, your base loses a life. When the base has none left, the game is over.
- In the scene Enemy, take the “Follow the path” node of step 3. From its Arrived output, add “Add to Variable”: variable base_life, value
-1. - Add “Destroy Object”: target enemy.
- Add “Condition”
=base_life <= 0. On the True output, add “Call event” with the event name game_over. - In the scene Game, add the “Local Event” event with the same name game_over.
- Add “Stop a timer”: Timer name (empty = all) =
waves(no more enemies are sent), then “Destroy by Tag”: Tag to destroy =enemy. - Add “Small message at the bottom of the screen” with the message Game over, then “Wait”
2seconds, then “Restart the scene”.
Play: build a tower or two with your credits, then watch them stop the enemies. Let a few get through to see the base lose its lives.
Going further
- Victory: count the enemies with a variable (+1 when one dies or arrives). When the number reaches the total you sent, show a message and win.
- Upgrade a tower: touch a tower to open a small window with a button “Upgrade” that costs credits and changes the cannon image and the speed of fire. Open the example Tower defense (cubes) to see how.
- Several enemy types: make a second scene (Enemy2) with more life and another speed, and let each wave send one or the other (open the example Tower defense (cubes) to see how).
- Crossroads: in the Path tool, touch a point and choose This point is a crossroads: the enemies pick a road at random, and the branches can join again.
- Several lanes and no traffic jams: in “Follow the path”, set Random side offset (pixels, ±: each one walks in its own lane) and Avoid the others: step aside to avoid overlapping and overtake slower ones (yes / no) to yes: the enemies walk side by side and overtake each other.
- A big map: make your map twice as wide, then add “Camera limits” and “Scroll the camera with a finger” at the start so the player scrolls the map with a finger.
- Sounds and sparks: add “Play sound” at each shot and an “Particle effect” when an enemy dies (see the guide Particles and effects).
If it doesn't work
- The enemies don't move: check that the Path name setting is the name of your path (Path), and that the path has a Start. Check that the scene Enemy has its “On Start” event.
- The tower doesn't shoot: check the tag enemy on the enemy, the name bullet in “Shoot an object”, and that the bullet object is in the scene Game (outside the screen, but not deleted). Raise Range in pixels, as a circle (0 = use the range in cells) if the enemies stay out of range.
- The bullets don't hurt: the bullet must have the tag bullet, and the event “If object touches Tag” must be in the scene Enemy, not in Game.
- The tower appears in the wrong place: for X and Y of “Place a copy of a scene”, use the green “Object value” nodes with The object involved in the event, as in step 7.
- The credits or the base lives don't change: check that the variables credits and base_life have the scope Global.