January 26, 2019
Tower Defense Tutorial
Episode 06 – Turrets
Youtube – How to make a Tower Defense Game (E06 BUILDING) – Unity Tutorial Youtube – How to make a Tower Defense Game (E07 CAMERA) – Unity Tutorial
By: Brackeys
Ep. 06
This tutorial covers the user interactions with the node spaces. We started with an OnMouseEnter and OnMouseExit methods. These simply changed the color of the node when hovering the mouse over the node object.
A BuildManager script was created and added to the GameManager object in the game to help dealing with building turrets. We want to access this to handle the building of turrets. The BuildManager used a basic singleton pattern. To do this, we created a public static BuildManager variable called instance within the BuildManager script itself. This will effectively reference itself. In the Awake method, we just set instance equal to this.
Building a turret just used the basic instantiation setup again. The only slight difference is that the node script references the BuildManager script to determine what turret to build (will be more useful in the future with different turret types).
SUMMARY
- OnMouseEnter and OnMouseExit very straightforward Unity methods for dealing with hovering mouse actions.
- Learn more about Singleton pattern and why it’s useful
- Basic color change on hover setup: store original color at start; change color during action (i.e. OnMouseEnter); change back to stored original color after event
- Easy to access public singleton-like class and its methods with the instance = this setup; Just {name of script}.instance.{name of method}()
- Setting a Color variable as public in Unity editor just lets you select any color with normal color selection palette
Ep. 07
This tutorial focused on creating the camera controller. It was created in sort of an RTS style. This would be down on keypress, using if (Input.GetKey(“w”)), which would then perform a transform.Translate(). Translate is an easy way to move objects without any need for physics.
To go with typical RTS camera movement, we wanted to move the camera around when the player has the mouse cursor around the edges of the screen. We started with Input.MousePosition, which registers the screen position of the mouse relative to the bottom left corner. This method of tracking the mouse obviously varies with the screen size, so it is a good idea to use this in conjunction with references to the screen size (i.e. Screen.height) as opposed to hardcoded values. We didn’t want to require the mouse to be at the complete border of the screen to move the camera, so we added a panBorderThickness variable to subtract from the screen dimension variables to create an area near the border of the camera where the movement would apply.
The camera movement buffer zones become the following:
- Top: mousePosition.y >= Screen.height – panBorderThickness
- Top: mousePosition.y <= panBorderThickness
- Top: mousePosition.x >= Screen.width – panBorderThickness
- Top: mousePosition.x <= panBorderThickness
We also added a zooming effect. It can feel extra nice if you rotate the camera relative to its zoom. There is an extra amendment to this tutorial for this effect. This will use the scroll wheel. It is treated more similarly to a joystick, where you use Input.GetAxis. You can find the reference name of the mouse scroll wheel in Project Settings -> Input. Finally, to restrict the zooming values, we created a min and max value and used Mathf.Clamp to limit that.
Unity Forum Link – More Information for Nicer Camera Controller
SUMMARY
- transform.Translate() good for non-Physics based movement
- Use Screen.Height and Screen.Width with a border buffer to create zones of mouse position to move camera in RTS-style
- Create min/max values for camera distance so it doesn’t fly away from game