Game Development Reference
In-Depth Information
Entering the shop
We can buy items from the shop and we can leave the shop, but how do we get into the
shop in the first place? As we did in Chapter 6 , The Big Wild World , we just need to add
trigger colliders where the user can enter the shop if they wish with the caveat that they
can only enter when they are in front of the shop and have pressed a key (the up arrow
button in this case).
To enable this, we need a very similar script to the NavigationPrompt script that we
used in Chapter 4 , The Game World , (always reuse) but with a few differences.
Create a new script named ShopEntry in Assets\Scripts\Navigation , then re-
place its contents to add a variable to control whether we can enter the shop or not; we do
this using the following code:
using UnityEngine;
public class ShopEntry : MonoBehaviour {
bool canEnterShop;
}
As with the Navigation script, we handle the changing of the state of this flag with a
single function. So, if we need to change anything else, we can do so using the following
code:
void DialogVisible(bool visibility)
{
canEnterShop = visibility;
MessagingManager.Instance.BroadcastUIEvent(visibility);
}
Next, we need trigger handlers to detect when the player is in front of a shop; refer to the
following code that tells you how to add trigger handlers:
void OnTriggerEnter2D(Collider2D col)
{
DialogVisible(true);
}
Search WWH ::




Custom Search