Tower Glower

Read Time:1 Minute, 51 Second
Tower Placement

Alright, so I wanted to make the towers placeable on the map. I added a Serialized Field called Is Waiting which is initially true.

I guard the Tower’s Update function against Is Waiting. This prevents the Tower from acquiring and shooting at targets until it is placed into the scene.

    private void Update() {
        // Bail if waiting to be placed.
        if (isWaiting) return;
        
        // Otherwise do target acquisition stuff.
    }

Tower Placement is handled by a method of the same name which sets the position of the tower and sets the Is Waiting field false. Once this method is called the guard method in Update will allow target acquisition to proceed.

    public void PlaceTower(Vector3 position) {
        transform.position = position;
        isWaiting = false;
    }

The Towers themselves are added to the Friendly Base component using the Inspector. They will be placed in order from element 0 to element n, I have to be sure to place them in the scene in the order I want them moved.

Placeable towers should be dragged into the list.
If the towers are out of order it will look weird as they are moved onto the game field.

It occurs to me that this should be a Queue and not a list. Meh. Here is the relevant code for retrieving Towers from the Friendly Base:

Inside the hierarchy of a Waypoint is a voxel mesh called FriendlyBlock. I’ve attached a Box Collider to this object which can be enabled via the Block Type Editor component. The collider is necessary to process the On Mouse Down callback method. I super dislike using a FindObject search here, but the goal is to avoid static members and stashing the reference in every block that gets clicked or instantiated seems excessive as well. Ideally, there should only be three Mouse Down clicks anyway.

As you can see, the method checks to see if towers are available, then it retrieves and places the tower in the scene. Note that the Transform of the Waypoint is used and not the Transform of the Friendly Block. This is significant because the pivot for the Waypoint is on top while the pivot on the Friendly Block is on the bottom.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleppy
Sleppy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *