Gameplay.LavaInteraction - robblofield/TomeboundDocs GitHub Wiki

Gameplay.LavaInteraction

Author: Shahrzad Beevis
Last Updated: 04/12/2023

Overview

This script enables a push block to be turned into a stepping stone when it makes contact with lava.

Dependencies

  • Prefab.Lava

Contents

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

Use case

This script is currently being used on all lava blocks to allow for a block to be turned into a stepping stone when it is pushed into the lava, allowing the player to move across.

Breakdown of Code

Using Statements

using UnityEngine;

Variables

public GameObject boxColliderGameObject;
public GameObject objectToActivate;
public string triggeringObjectTag = "Block";

public GameObject boxColliderGameObject:

Variable that is assigned in the inspector allowing for BoxCollider component on this object to be changed in response to the trigger event - in this case it is the box collider of the lava block that stops the player from crossing the lava.

public GameObject objectToActivate:

Variable that is assigned in the inspector - in this case for the stepping stone block which allows player to move across lava when activated.

public string triggeringObjectTag = "Block":

Represents the variable tag that triggers the interactions in the script

Class Declaration

public class LavaInteraction : MonoBehaviour

OnTriggerEnter(Collider other)

private void OnTriggerEnter(Collider other)
 {
     
     if (other.CompareTag(triggeringObjectTag))
     {
         
         if (objectToActivate != null)
         {
             objectToActivate.SetActive(true);
         }
         
         if (boxColliderGameObject != null)
         {
             BoxCollider boxCollider = boxColliderGameObject.GetComponent<BoxCollider>();
             if (boxCollider != null)
             {
                 boxCollider.gameObject.SetActive(false);
             }
                
         }
         other.gameObject.SetActive(false);
     }
 }

This method is automatically called when a collider enters the trigger zone of the GameObject it is attached to. It checks whether the GameObject that entered the trigger zone has the same tag as that stored in the triggeringObjectTag. If the entering GameObject has the expected tag - in this case "Block", it checks that the variable objectToActivate is not null (objectToActivate in this situation is the stepping stone). If it is not null it sets the state of objectToActivate to true.

It then also checks that the BoxCollider component was successfully obtained (this collider is what stops the player moving over the lava when there is no stepping stone) - if this is also not null the BoxCollider's active state is set to false, allowing the player to move through it and cross the lava using the now active stepping stone. Finally it deactivates the GameObject that entered the trigger zone.

Future Expansion

N/A

Full Code Reference


using UnityEngine;

public class LavaInteraction : MonoBehaviour
{
    public GameObject boxColliderGameObject;
    public GameObject objectToActivate;
    public string triggeringObjectTag = "Block";

    private void OnTriggerEnter(Collider other)
    {
        
        if (other.CompareTag(triggeringObjectTag))
        {
            
            if (objectToActivate != null)
            {
                objectToActivate.SetActive(true);
            }
            
            if (boxColliderGameObject != null)
            {
                BoxCollider boxCollider = boxColliderGameObject.GetComponent<BoxCollider>();
                if (boxCollider != null)
                {
                    boxCollider.gameObject.SetActive(false);
                }
                   
            }
            other.gameObject.SetActive(false);
        }
    }

    
}