Action_EnterGate - robblofield/TomeboundDocs GitHub Wiki

Action_EnterGate

Author: Youngju Yun
Last Updated: 02/01/2024

Overview

A script holds the data and functions for player characters when they enter the gate to the different floors. It forms part of the "Action"

Dependencies

  • Player.cs
  • Gate.cs
  • GameObject wizard X/Y
  • GameObject gateChecker;

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Variables
    • checkGateColliding()
    • OnDrawGizmosSelected()
    • EnterGate()
  • Future Expansion
  • Full Code Reference

Use case

The script is used when the player tries to enter the gate leading to the other floors. This script is not standalone and the function must be executed from other scripts like Player.cs.

Breakdown of Code

Using Statements

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Class Declaration

public class C#ScriptTitle : MonoBehaviour

If the script is inheriting from anything other than MonoBehaviour - discuss it here.

Variables

    public Transform gateChecker;
    public float sphereRadius;
    public LayerMask layerMaskGate;
    private GameObject collidingGate;
    private float currentHitDistance;

Transform gateChecker: A reference to the Transform data of one of player character's children called "GateChecker".

float sphereRadius: It determines the size of the SphereCast used in the checkGateColliding() function. The size can be previewed on OnDrawGizmosSelected().

LayerMask layerMaskGate: A variable is used in the function checkGateColliding() and it should select only a layer "Gate" to locate only the Gate GameObject colliding with it.

GameObject collidingGate: A local GameObject variable to store the Gate which is colliding with the SphereCast.

float currentHitDistance: A local value which stores the scaler distance between the character and the collidingGate.

checkGateColliding()

    private bool checkGateColliding()
	{
		RaycastHit[] hits = new RaycastHit[10];
		int numberOfHits = Physics.SphereCastNonAlloc(gateChecker.position, sphereRadius, Vector3.down, hits, 5f, layerMaskGate, QueryTriggerInteraction.UseGlobal);
		if (numberOfHits > 0)
		{
			collidingGate = hits[0].collider.gameObject;
			Gizmos.color = Color.blue;
			currentHitDistance = hits[0].distance;
			return true;
		}
		// Debug.Log("No Gate Detected");
		return false;
	}

The purpose of this function find the closest Gate GameObject within the range of the specified SphereCast.

Firstly it creates an array of RaycastHits with a length of 10. Then it uses Physics.SphereCastNonAlloc to cast spheres around the player characters and store all data of GameObjects with a layer specified in the layerMaskGate variable.

if it finds more than one RaycastHits colliding within the sphere, the variable collidingGate will store the data of gameObject which is in the first queue of the array, meaning that it is the closest to the player character.

Then it stores the distance between the character and the Gate in currentHitDistance and returns true.

If numberOfHits is 0, it means it found no Gate Objects and it returns false.

OnDrawGizmosSelected()

	private void OnDrawGizmosSelected()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawWireSphere(gateChecker.position + Vector3.zero, sphereRadius);
	}

A debug function to display the wired SphereCast while in Play mode.

EnterGate()

    public void EnterGate()
	{
		if (checkGateColliding())
		{
			collidingGate.GetComponent<Gate>().TeleportObject();
		}
	}

A function which is executed by the player. It checks nearby Gate by executing the checkGateColliding function then if it finds the Gate Object it sends a request to the Gate to execute the TeleportObject() function throughout its Gate.cs script.

Future Expansion

So far the script is exclusive to the player characters. However, if there is an NPC who can enter the gate as the player does, the Action_EnterGate.cs will be compatible for the NPC as well.

Full Code Reference

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Action_EnterGate : MonoBehaviour
{
    public Transform gateChecker;
    public float sphereRadius;
    public LayerMask layerMaskGate;
    private GameObject collidingGate;
    private float currentHitDistance;

    private bool checkGateColliding()
	{
		RaycastHit[] hits = new RaycastHit[10];
		int numberOfHits = Physics.SphereCastNonAlloc(gateChecker.position, sphereRadius, Vector3.down, hits, 5f, layerMaskGate, QueryTriggerInteraction.UseGlobal);
		if (numberOfHits > 0)
		{
			collidingGate = hits[0].collider.gameObject;
			Gizmos.color = Color.blue;
			currentHitDistance = hits[0].distance;
			return true;
		}
		// Debug.Log("No Gate Detected");
		return false;
	}

	private void OnDrawGizmosSelected()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawWireSphere(gateChecker.position + Vector3.zero, sphereRadius);
	}

    public void EnterGate()
	{
		if (checkGateColliding())
		{
			collidingGate.GetComponent<Gate>().TeleportObject();
		}
	}

}
⚠️ **GitHub.com Fallback** ⚠️