Gameplay.FillBarrel - robblofield/TomeboundDocs GitHub Wiki

Gameplay.FillBarrel

Author: Shahrzad Beevis
Last Updated: 14/05/2024

Overview

A script that manages the behaviour of filling a barrel when a waterfall collides with it.

Dependencies

N/A

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Class Declaration
    • Variables
    • Method/OnTriggerEnter(Collider other)
    • Method/OnTriggerExit(Collider other)
    • Method/Update()
    • Method/IsWaterfallColliding()
    • Method/ActivateObjects()
    • Method/DeactivateObjects()
  • Future Expansion
  • Full Code Reference

Use case

This script facilitates the management of GameObject activation and deactivation based on collisions between a barrel and waterfalls to simulate the effect of a barrel filling with water.

Breakdown of Code

Using Statements

using UnityEngine;

Class Declaration

public class FillBarrel : MonoBehaviour

Variables

public GameObject[] objectsToActivate;
public GameObject[] objectsToDeactivate;
public string waterfallTag = "Waterfall";

private bool isWaterfallColliding = false;

public GameObject[] objectsToActivate:

An array of GameObjects that should be activated when the waterfall collides with the barrel.

public GameObject[] objectsToDeactivate:

An array of GameObjects that should be deactivated when the waterfall collides with the barrel.

public string waterfallTag = "Waterfall":

The tag of the GameObject representing the waterfall.

private bool isWaterfallColliding = false:

A boolean flag to track whether the barrel is currently colliding with the waterfall.

OnTriggerEnter(Collider other)

private void OnTriggerEnter(Collider other)
{
    if (other.CompareTag(waterfallTag))
    {
        isWaterfallColliding = true;
        ActivateObjects();
    }
}

This method is invoked when another Collider enters the trigger collider attached to the barrel GameObject. If the entering collider has the tag 'Waterfall', it sets isWaterfallColliding to true, indicating the presence of a waterfall collision, and activates the specified objects.

OnTriggerExit(Collider other)

private void OnTriggerExit(Collider other)
{
    if (other.CompareTag(waterfallTag))
    {
        isWaterfallColliding = false;
        DeactivateObjects();
    }
}

This method is called when a Collider exits the trigger collider attached to the barrel GameObject. If the exiting collider has the tag 'Waterfall', it sets isWaterfallColliding to false, indicating the end of the waterfall collision, and deactivates the specified objects.

Update()

private void Update()
{
    if (isWaterfallColliding && !IsWaterfallColliding())
    {
        isWaterfallColliding = false;
        DeactivateObjects();
    }
}

This method is executed every frame. It checks whether the isWaterfallColliding flag is true and if the waterfall is still colliding with the barrel. If the waterfall is no longer colliding, it sets isWaterfallColliding to false and deactivates the specified objects.

IsWaterfallColliding()

private bool IsWaterfallColliding()
{
    Collider[] colliders = Physics.OverlapBox(transform.position, transform.localScale / 2f);
    foreach (Collider col in colliders)
    {
        if (col.CompareTag(waterfallTag))
        {
            return true;
        }
    }
    return false;
}

This method uses Physics.OverlapBox to check if any colliders with the Waterfall tag are currently overlapping the barrel. It returns true if the barrel is colliding with a waterfall, otherwise false.

ActivateObjects()

 private void ActivateObjects()
 {
     foreach (GameObject obj in objectsToActivate)
     {
         if (obj != null)
         {
             obj.SetActive(true);
         }
         else
         {
             Debug.LogWarning("No object specified to turn on.");
         }
     }
 }

This method iterates through the objectsToActivate array and activates each GameObject in the list. It provides a mechanism to enable specific visual or functional elements associated with the waterfall interaction.

DeactivateObjects()

private void DeactivateObjects()
{
    foreach (GameObject obj in objectsToDeactivate)
    {
        if (obj != null)
        {
            obj.SetActive(false);
        }
        else
        {
            Debug.LogWarning("No object specified to turn off.");
        }
    }
}

This method iterates through the objectsToDeactivate array and deactivates each GameObject in the list. It allows for the disabling of specific elements when the waterfall interaction ends, providing control over the scene's visual components.

Future Expansion

N/A

Full Code Reference

using UnityEngine;

public class FillBarrel : MonoBehaviour
{
    public GameObject[] objectsToActivate;
    public GameObject[] objectsToDeactivate;
    public string waterfallTag = "Waterfall";

    private bool isWaterfallColliding = false;

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag(waterfallTag))
        {
            isWaterfallColliding = true;
            ActivateObjects();
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag(waterfallTag))
        {
            isWaterfallColliding = false;
            DeactivateObjects();
        }
    }

    private void Update()
    {
        if (isWaterfallColliding && !IsWaterfallColliding())
        {
            isWaterfallColliding = false;
            DeactivateObjects();
        }
    }

    private bool IsWaterfallColliding()
    {
        Collider[] colliders = Physics.OverlapBox(transform.position, transform.localScale / 2f);
        foreach (Collider col in colliders)
        {
            if (col.CompareTag(waterfallTag))
            {
                return true;
            }
        }
        return false;
    }

    private void ActivateObjects()
    {
        foreach (GameObject obj in objectsToActivate)
        {
            if (obj != null)
            {
                obj.SetActive(true);
            }
            else
            {
                Debug.LogWarning("No object specified to turn on.");
            }
        }
    }

    private void DeactivateObjects()
    {
        foreach (GameObject obj in objectsToDeactivate)
        {
            if (obj != null)
            {
                obj.SetActive(false);
            }
            else
            {
                Debug.LogWarning("No object specified to turn off.");
            }
        }
    }
}