Tower Defense Tutorial - Brackeys - Ep. 04

January 24, 2019

Tower Defense Tutorial

Episode 04 - Turrets

Youtube - How to make a Tower Defense Game (E04 TURRETS) - Unity Tutorial
By: Brackeys
Ep. 04

This starts the creation and implementation of the turret objects.

Use OnDrawGizmosSelected to show important editor information when an object is selected. In this case, DrawWireSphere was used and it showed a wireframe of a sphere with a radius corresponding to the range of the turret.

Wanted turret to update to find targets to fire at, so method UpdateTarget was created. This is going to be relatively expensive using distance checks so it was made as a separate “Update” method so we could run it much less than every single frame. This method was then called in Start with an InvokeRepeating call, which lets you set a time to start running, and a time interval to repeat the method.

Rotating the turrets required usage of Quaternions. First, the direction was obtained with a simple Vector3 between the object and target. This was converted to a Quaternion variable with Quaternion.LookRotation(). It was then converted back to an angular measurement, which was another Vector3 variable that took the Quaternion and just applied .eulerAngles to it. Finally, that angular value was passed specifically to the transform’s rotation value as a Quaternion.Euler rotation vector (which we just passed the y portion of our rotation vector to the y portion of this rotation vector). So keep in mind dealing with angular movement may require lots of back and forth with angles and quaternions.

PROBLEMS

There was an issue where the turret rotation was off by 90 degrees, so the tutorial solved this by separating out the prefab and rotating the rotationPoint(partToRotate) that was an empty parent object of the intended object needing rotation by -90. The child/parent relationship was then put back together and the prefab was reassembled. I decided to just add 90 degrees to the y rotation of partToRotate in the script when assigning the rotation and this worked fine. However, when we changed the method to add a Lerp for the rotation, this broke my solution. This makes sense because the Lerp wanted to go between the partToRotate original rotation and the new rotation, neither of which had my additional 90 degrees in it. Then when it would go to assign the new partToRotate.rotation with my additional 90 degrees and go back to Lerp again, the value was then way out of the original range. This gave a crazy looking jittery effect. This has taught me that I don’t understand Quaternions at all and will need to look into those in the future.

Comments

Popular posts from this blog

Online Multiplayer Networking Solution Tutorial Using Unity and Mirror - Tutorial by: Jason Weimann

Sebastian Lague A* Tutorial Series - Path Smoothing - Pt. 08 and Pt. 09

Designing Pixel Art Color Palettes in Aseprite by AdamCYounis