Game Development Reference
In-Depth Information
Journeying onwards
Now that we have collision zones on our town's borders, we can hook into this by using a
script to activate when the hero approaches.
Create a new C# script called NavigationPrompt , clear its contents, and populate it
with the following code:
using UnityEngine;
public class NavigationPrompt : MonoBehavior {
bool showDialog;
void OnCollisionEnter2D(Collision2D col)
{
showDialog = true;
}
void OnCollisionExit2D(Collision2D col)
{
showDialog = false;
}
}
The preceding code gives us the framework of a collision detection script that sets a flag
on and off if the character interacts with what the script is attached to, provided it has a
physics collision component. Without it, this script would do nothing and it won't cause an
error.
Next, we will do something with the flag and display some GUI when the flag is set. So,
add the following extra function to the preceding script:
void OnGUI()
{
if (showDialog)
{
//layout start
GUI.BeginGroup(new Rect(Screen.width / 2 - 150, 50,
Search WWH ::




Custom Search