Game Development Reference
In-Depth Information
if (inputActive && Input.GetMouseButtonUp(0))
{
StartLocation = transform.position.ToVector3_2D();
timer = 0;
TargetLocation =
WorldExtensions.GetScreenPositionFor2D(Input.mousePosition);
startedTravelling = true;
//Work out if a battle is going to happen and if it's
likely
//then set the distance the player will travel before
it
//happens
var EncounterProbability = Random.Range(1, 100);
if (EncounterProbability < EncounterChance &&
!GameState.playerReturningHome)
{
EncounterDistance = (Vector3.Distance(StartLocation,
TargetLocation) / 100) * Random.Range(10, 100);
}
else
{
EncounterDistance = 0;
}
}
Tip
At this point, you will notice that the code between touch and click is becoming duplic-
ated; try to refactor this (clean it up) so less code is duplicated. There is no spoon.
We know that if an event occurs and when it does, all that is left is to act on it. So, in the
Update method where we animate the player across the screen, we simply need an addi-
tional check to see whether we have travelled to the event and if so, stop and enter the
battle. We can do so using the following code:
Search WWH ::




Custom Search