Game Development Reference
In-Depth Information
The first
if
statement will allow the player to shoot the projectile. To shoot it, we
instantiate the GameObject and the player's location and rotation. To let the player
pause the game, use an input that we created in
Chapter 1
,
Interactive Input
. Setting
the
timeScale
object to
0
will pause any script that uses the
Update
function, and
setting it to
1
will resume it.
Collecting potions
To collect potions, we will need to add a collision function, as follows:
void OnTriggerEnter(Collider other)
{
if(other.tag == "Potion")
{
GetComponent<Inventory>().AddToInventory(1,
Potion);
for(int i = 0; i <
GetComponent<GUI_2D>().Items.Count; i ++)
{
if(GetComponent<GUI_2D>().Items[i].name
== "")
GetComponent<GUI_2D>().Items[i] =
Potion;
break;
}
Destroy(other.gameObject);
}
}
To collect the potion, we check the trigger's tag to make sure it is the potion. Next,
we add it to the inventory by calling the
AddToInventory
function of the
Invent-
ory
component, which we will be adding later. Also, we add it to the GUI by calling
the
Items
array from the
GUI_2D
component and assigning the potion. Finally, we
destroy the
GameObject
potion in the game world so that the player can no longer
get that potion.