To be able to detect when the player has clicked on a tile, you will add a new event to the TileController
that can be used to forward click events.
Add an UnityEvent<TileController> OnCursorClick
property to your TileController
Verify that the event is now available in the inspector.
NotifyCursorClicked()
method that invokes OnCursorClick
OnClick
event to invoke the NotifyCursorClicked
method.Now that you have an event that sends an event when a tile is clicked, you add in a Turret Spawner that will listen for clicks. If the tile is not occupied, it will spawn a turret at that location.
MonoBehaviour
called TurretSpawner
GameObject TurretPrefab
property to your TurretSpawner
[field: SerializeField]
attribute to the TurretPrefab
propertyGameObject
in the Turret Spawn Test Scene, this will be your TurretSpawnerTurretSpawner
componentTurretPrefab
to use the prefab you created for your TurretSpawnTurret(TileController)
method to your TurretSpawner
SpawnTurret
, check to see if the specified TileController
IsOccupied
. return
)Instantiate
a TurretPrefab
and set the position to match the position of the TileController
Before writing a handler to register your TurretSpanwer
on all tiles, it is helpful to test if the SpawnTurret
method is working against a single tile.
Tile
OnCursorClick
eventTurretSpanwer
as the listening objectTurretSpawner.SpawnTurret
method as the method to invokeYou likely noticed that after you placed your turret, the cursor stops registering on tiles that are near your turret. This is because the collider the turret is using to detect enemies is blocking the tile. To prevent this, you can create Collision Layers for your objects and specify which layers mouse events detect.
From the top menu select Edit
> Project Settings
On the left side, select the Tags and Layers
tab
If necessary, open the Layers
drop down. (Ensure you are not in Sorting Layers
/ Rendering Layers
)
Add a Tiles
layer
Prefabs > Tiles
folderLayer
to Tile
Tile
layer to all of your tile objects and their modelsIf you switch to the Game tab or enter Play Mode you will no longer see your tiles. This is because the Camera does not render them.
Culling Mask
to see the Tile
layerBy default, the Camera will detect all collision layers. This means that your mouse is being blocked by your turrets area of engagement collider. To fix this, you must change which layers the camera allows the mouse to interact with. This is called the Camera's Event Layer.
Unfortunately, there is no way to set this in the Inspector. But, it is possible to modify the Camera.eventMask
field using a script.
MonoBehaviour
called CameraEventMaskController
LayerMask EventMask
propertyCameraEventMaskController
EventMask
to Tile
Awake()
method to your CameraEventMaskController
Awake()
GetComponent<Camera>()
to find the Camera
componentcamera
's eventMask
to the EventMask
propertyNow that you have verified that you can spawn a single turret, you are ready to listen to all of the tiles in a grid.
TileCursor
TurretSpawner
such thatWhen you have finished, you should be able to click on any tile in the scene to spawn a turret:
Currently, if you click the same tile multiple times, it will spawn multiple turrets. This is because you never set tilecontroller.IsOccupied
to true
after spawning a turret in SpawnTurret
Can you fix this bug?
SpawnTurret
to set tileController.IsOccupied
to true
TileController
Now that you are able to spawn turrets on a tile, it is time to add a TileCursor and a TurretSpawner
to your map. In the next lesson, you will learn how to add a button that enables the player to build a turret in your level.