Tower Defense Tutorial – Brackeys – Ep. 05

January 25, 2019

Tower Defense Tutorial

Episode 05 – Turrets

Youtube – How to make a Tower Defense Game (E05 SHOOTING) – Unity Tutorial

By: Brackeys
Ep. 05

This tutorial starts adding the shooting logic to the turrets. It begins with a basic setup to have a fire rate/firing cooldown based on a timer subject to Time.deltaTime. It then gets into some variable organization where they are situated in a way that makes more sense and adds in headers for clarification in the Unity editor. Creating a header just requires this: [Header(“Header Name”)]. This really nicely separates the variables in the editor, as it sections them off along with the title at the top of that block of variables.

The bullets for the turret are created with a basic Instantiation. However, since we want to reference the bullet instantiated in someway, we set it equal to a GameObject variable. This also requires “object casting”, so the instantiation has (GameObject) in front of it. I need to look into object casting.

This required more instances of destroying gameObjects, and again the tutorial adds the return command afterward to make sure to account for the processing time needed to destroy an object. I need to look into proper use of “return” in Unity scripting.

Determining if the bullet hit the target used an interesting method. The vector between the bullet and its target was determined each frame in Update as normal, but instead of using this solely for directional purposes by normalizing it, the distance (magnitude) was used to determine collision. Another variable, distanceThisFrame, was created and was just the bullet’s speed * time.deltatime. This would determine how far the bullet should move this frame. This was compared with that directional vector between the bullet and the target, and if the distance between them was less than the distance the bullet should travel, it was determined the bullet should collide. I think this specifically works better with a “homing” style projectile, since this means the target moving out of the way isn’t really an option anyway, and it constantly wants to update direction anyway.

They created a simple particle effect to go with the bullet collision. They treated this similarly to the instantiation of the bullet. The particle effect was instantiated as a new gameObject variable, and cast as a gameObject. This was then immediately referenced the next line to Destroy it after a time (after its duration basically). This is a good way to keep your scene clean with instantiated particle effects so they don’t linger as gameObject even after they appear to be gone.

SUMMARY

  • Headers are fantastic for keeping public variables organized in the Unity editor.
  • Destroying gameObjects can take processing time, so using return in conjunction can help keep code safe and do what you expect.
  • Look into proper use of “return;” command.
  • Look into “casting to GameObject”
  • Instance particle effects as gameObject variables to reference to easily destroy them so they do not clog scene