Game Development Reference
In-Depth Information
Managing input order
If you run the code at this point, you will notice that all the changes we just made did not
actually fix the problem. When we click on the play button, the player still moves to the
click position. The reason for this is very simple: your machine is just too darn quick.
Basically, when you click on the GUI button, the UI event is fired. In the
MapMovement
script, however, it receives this straightaway and changes the
inputActive
flag. Then,
when the
Update
method is called, the script thinks the UI has already gone away and
receives the same click action and then proceeds to move the player.
We can handle this in one of two ways—either we can change the script execution order
(visit
http://docs.unity3d.com/Manual/class-ScriptExecution.html
for more details, but
this can become quite messy to manage), or we can simply update the
inputActive
flag at the end of the
Update
loop.
To keep things simple, let's do the latter. So, create the following new property at the top
of the
MapMovement
script:
bool inputReady = true;
Then, instead of updating the
inputActive
flag directly, you would update the new
flag instead. This allows us to delay the change in the input status for the script. So, up-
date the
UpdateInputAction
method as follows:
private void UpdateInputAction(bool uiVisible)
{
inputReady
= !uiVisible;
}
Finally, at the end of the
Update
method, we would set the
inputActive
flag to the
value of the new
inputReady
flag after checking all the user input and allowing the
screen prompt to close first:
inputActive = inputReady;
Now when you run the project and the GUI is displayed, clicking on the play button no
longer causes the player to move as well.