Hello! I’m trying to to add a function where the Player is able to swim when they pick up an item called “snorkel” and is knocked back when not. I already have the Player Inventory script where it stores the item data but I’m not sure how to call upon it.
This is the script I have within the Player script right now. Individually the functions seem to work but I’m not sure how to phrase it together.
private void OnTriggerEnter2D(Collider2D other)
{
// With SNORKEL
if (other.gameObject.tag == "Water" /*&& Inventory has snorkel*/)
{
Debug.Log("SWIMMING!");
anim.SetBool("IsSwimming", true);
isSwimming = true;
}
// Without SNORKEL
if (other.gameObject.tag == "Water" /*&& Inventory does NOT have snorkel*/)
{
Debug.Log("CAN'T SWIM!");
anim.SetBool("IsHurting", true);
isHurting = true;
isSwimming = false;
//knockback Player
StartCoroutine(this.KnockBack(0.04f, 700, this.transform.position));
}
}
private void OnTriggerExit2D(Collider2D other)
{
//With SNORKEL
if (other.gameObject.tag == "Water" /*&& Inventory has snorkel*/)
{
anim.SetBool("IsSwimming", false);
isSwimming = false;
}
// Without SNORKEL
if else (other.gameObject.tag == "Water" /*&& Inventory does NOT have snorkel*/)
{
anim.SetBool("IsHurting", false);
isHurting = false;
isSwimming = false;
}