During game play, it will be helpful to your player to be able to tell more easily which tile they are hovering / selecting. To do this, you can use the selection-a
or selection-b
model which fit nicely around a tile.
model
child of the Prefab (I will be using selection-a
)Currently, your Tile Prefab has no components attached to it. When placing a turret, it will be useful to track if the Tile
is occupied. To do this, create a TileController
MonoBehaviour Script that has a property bool IsOccupied
. By default, this will be false
.
TileController
MonoBehaviour Scriptbool IsOccupied
that is false by defaultTileController
component to your Tile PrefabThe TileCursor
will need to listen to your TileController
s to know if the mouse has entered them. To do this, you will add an event to your TileController
that will notify listeners when the mouse has entered.
UnityEvent<TileController> OnCursorEnter
model
's MouseEvents
You may have noticed that you cannot call TileController.OnCursorEnter.Invoke
from the MouseEvents.OnEnter
listener in the Inspector. This is because you must provide a TileController
to call OnCursorEnter.Invoke
. However, you can create a method that invokes it that takes no arguments.
Add a method NotifyCursorEnter()
In NotifyCursorEnter()
invoke OnCursorEnter
with this
In the Inspector, add a listener to MouseEvents.OnEnter
that will invoke TileController.NotifyCursorEnter()
With your TileController
propagating itself to listeners, you can now register your TileCursor
on them and update its position to match.
TileCursor
MonoBehaviour ScriptHandleTileEntered(TileController)
methodHandleTileEntered
, set the transform.position
of the cursor to match the transform.position
of the TileController
.Tile Controller
componentOnCursorEnter
eventBy using a Unity Event that sends a TileController
, you gain access to Dynamic TileController functions in the Function Drop Down. When you use this function, it will send the TileController
as an argument to the specified method.
To move the Tile Cursor to the most recently entered Tile, you need to register the Tile Cursor on all of your Tile Controller
s. Doing this through the Hierarchy and Inspector is challenging and error prone. To make things worse, if you decide to change your map, you have to do it again! Remember, as a programmer, any time you find yourself performing a repetitive task, you should ask yourself if there exists a way to automate it. If not, you should consider how difficult it would be to create your own automation.
Because all of your TileController
s are children of your Grid, you can retrieve them all using the GetComponentInChildren
method which retrieves all of the components of a specified type from the specified GameObject and all of its children.
GameObject TargetGrid
to your TileCursor
ListenToTilesIn(GameObject)
method to your TileCursor
ListenToTileIn
, iterate through all of the TileController
components and add a listener to call HandleTileEntered
OnEnable()
method to TileCursor
OnEnable
, call ListenToTilesIn(TargetGrid)
TargetGrid
for your TileCursorRecall that it is good practice to remove a listener if the listening object becomes inactive (e.g. OnDisable
).
StopListeningToTilesIn(GameObject grid)
methodStopListeningToTiles
you should iterate through the specified grid searching for TileController
s and call OnCursorEnter.RemoveListener
on each one found.OnDisable
methodOnDisable
, call StopListeningToTiles
on the TargetGrid
Currently, when the player moves the mouse off of map into a space on the screen that does not contain a tile, the TileCursor remains on the most recently entered Tile. Instead, you should hide the Tile Cursors model
.
UnityEvent<TileController> OnCursorExit
property to your TileController
NotifyCursorExit()
method that invokes OnCursorExit
GameObject Model
property to your TileCursor
HandleTileExited(TileController)
method to TileCursor
HandleTileExited
, call Model.SetActive(false)
to hide the TileCursor
's modelHandleTileEntered
, call Model.SetActive(true)
to show the TileCursor
's modelOnEnable
, and OnDisable
When you have successfully completed this challenge, your test scene should look similar to this:
With the ability to easily see which tile the player is interacting with, it is time to implement an OnCursorClicked
event and an OnTileSelected
method that will allow your player to spawn a turret. 15 - Turret Spawner