← Yop2D

How to make a tower defense?

Beginner guide · no code

Français · English · Español · Deutsch · Italiano · Português · Русский · 中文 · 日本語

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.

You need:
👉 This guide is written for a 1920 x 1080 landscape game. The example Tower defense (cubes) (Examples tab) contains ready-made images in the folder Images/TD: open it and copy the ones you like into your project. You can also draw your own, the guide works with any style. The scenes Enemy and Tower are copied into the game many times: see the guide Ready-made objects (prefabs).

Step 1: the variables

  1. 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:

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.

  1. Open the Assets window (Resources panel) and touch Path. The path editor opens, with your scene in the background.
  2. Touch New path and name it Path.
  3. 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.
  4. Put the last point on your base: it has no exit, it is the End.
  5. Touch Watch it go to see a dot travel along the path. Then close the editor: the path is saved by itself.
👉 To change the start, touch a point and choose Make this point the start. To move a point, use the tool Move; to remove one, use Erase.

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.

  1. Create a new scene named Enemy (menu +, then Create a scene).
  2. In the scene Enemy, add an Image named enemy at X = 0, Y = 0, and give it the tag enemy.
  3. Open the Blueprint of the scene Enemy and add the “On Start” event.
  4. Add “Give health to the object”: target enemy, Health points = 3 (the enemy takes 3 hits).
  5. Add “Follow the path”: target enemy, Path name = Path, Speed (pixels per second) = 150.
  6. Link the three nodes in order.
On Start
→
Give health to the objectenemy · 3
→
Follow the pathenemy · Path · 150

Step 4: send the waves

Now go back to your main scene (Game) and send enemies one after the other.

  1. Open the Blueprint of the scene Game and add the “On Start” event.
  2. Add two “Modify Variable” nodes: credits = 150, then base_life = 20. They start every new game clean, even after a Game Over.
  3. Add “Wait”: Duration (seconds) = 3 (a pause before the first enemy).
  4. Add “Repeat”: Every (seconds) = 1.5, Number of times (0 = endless) = 15, Timer name (optional) = waves.
  5. 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.
  6. Link the nodes in order.
On Start
→
Modify Variablecredits · 150
→
Modify Variablebase_life · 20
→
Wait3
→
Repeat1.5 · 15
Each time →
Place a copy of a sceneEnemy

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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).
  6. Add “Cooldown”: target cannon, Delay (seconds) = 1.2 (one shot every 1.2 seconds at most).
  7. 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.
On Start
→
Repeat0.2 · endless
Each time →
Choose a targettower · enemy · 330
Target found →
Face towardscannon
→
Cooldowncannon · 1.2
Ready →
Shoot an objectcannon · bullet
👉 Place a Tower by hand into the scene Game to test it (menu + → the scene Tower): it should shoot at every enemy that passes within 330 pixels.

Step 6: hit the enemies

This part goes in the scene Enemy: it is the enemy that knows when a bullet touches it.

  1. Open the Blueprint of the scene Enemy and add the “If object touches Tag” event: target enemy, Tag bullet.
  2. Add “Destroy Object” and choose The object involved in the event as the target: it is the bullet, which disappears.
  3. Add “Deal damage”: target enemy, Damage = 1. By default the enemy is destroyed when its life reaches zero.
  4. From the Out of health output, add “Add to Variable”: variable credits, value 15 (the reward).
  5. Link the nodes in order.
If object touches Tagenemy · bullet
→
Destroy Objectbullet
→
Deal damageenemy · 1
Out of health →
Add to Variablecredits · 15

Step 7: build towers

The player builds a tower by touching a build spot. It costs 50 credits.

  1. Select each build spot and give it the tag spot in the Inspector.
  2. In the scene Game, add the “On Object Click” event and choose # spot as the object (every object with the tag spot).
  3. Add “Condition” =credits >= 50.
  4. On the True output: “Add to Variable”: variable credits, value -50.
  5. 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 with y: the tower appears exactly where the player touched.
  6. Add “Destroy Object” with The object involved in the event as the target: the build spot disappears.
  7. On the False output: “Small message at the bottom of the screen” with the message Not enough credits.
On Object Click# spot
→
Condition=credits >= 50
True →
Add to Variablecredits · -50
→
Place a copy of a sceneTower
→
Destroy Objectspot

Step 8: the numbers on screen

  1. In the scene Game, add the “Every frame” event.
  2. Add “Modify Text”: target creditsText, New text = ="Credits: " + credits.
  3. Add a second “Modify Text”: target lifeText, New text = ="Base: " + base_life.
  4. Link the three nodes in order.
Every frame
→
Modify TextcreditsText
→
Modify TextlifeText

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.

  1. 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.
  2. Add “Destroy Object”: target enemy.
  3. Add “Condition” =base_life <= 0. On the True output, add “Call event” with the event name game_over.
  4. In the scene Game, add the “Local Event” event with the same name game_over.
  5. Add “Stop a timer”: Timer name (empty = all) = waves (no more enemies are sent), then “Destroy by Tag”: Tag to destroy = enemy.
  6. Add “Small message at the bottom of the screen” with the message Game over, then “Wait” 2 seconds, then “Restart the scene”.
Follow the pathenemy
Arrived →
Add to Variablebase_life · -1
→
Destroy Objectenemy
→
Condition=base_life <= 0
True →
Call eventgame_over
Local Eventgame_over
→
Stop a timerwaves
→
Destroy by Tagenemy
→
Small message at the bottom of the screenGame over
→
Wait2
→
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

If it doesn't work