Game Development Reference
In-Depth Information
private bool selected;
GameObject selectionCircle;
Now, to liven the selection process a bit, let's add some spin to the selection circle when it
is on the screen. To do this, we'll add a simple coroutine to constantly update the selection
circles' rotation transform (simple and effective). We could have used the 2D animation
system to do the same thing, but it's a bit too much for a simple rotation (unless you want
to do more fancy things with the selection circle, such as add particles, have the circle
jump up and down while spinning, and so on).
So, in the
EnemyController
script, add the following coroutine function:
IEnumerator SpinObject(GameObject target)
{
while (true)
{
target.transform.Rotate(0, 0, 180 * Time.deltaTime);
yield return null;
}
}
Nothing fancy; you just need to rotate the object on its
z
axis over time.
If you want the circle to spin faster or slower, just alter the amount of
z
axis rotation you
apply. Here, I have it set to spin 180 degrees every second, one full spin every 2 seconds.
Next, when the player clicks, we use the combination of the
BoxCollider2D
and
OnMouseDown
functions to select the Goblin and display the selection circle.
Add a new
BoxCollider2D
component to the
Goblin
prefab and then add the follow-
ing function to the
EnemyController
script:
void OnMouseDown()
{
if (battleManager.CanSelectEnemy)
{
var selection = !selected;
battleManager.ClearSelectedEnemy();
selected = selection;
if (selected)