Een technisch overzicht van teleportatie mechanics in Unity.
Voor een groepsproject had ik een teleporter gemaakt. Waarmee het de voorwarden had dat er ook nog een cooldown op zat en dat heb ik gedaan.
De TeleportManager fungeert als een singleton die de globale status van teleportatie beheert, zoals de brandstofstatus en de cooldown-tijd.
public class TeleportManager : MonoBehaviour
{
public static TeleportManager Instance;
// These bools check if the player can teleport or not.
public bool isFueled = false;
public bool isCooldown = false;
/// <summary>
/// We don't want the player to keep teleporting so with this we can control the time in which he can teleport.
/// </summary>
public float cooldownTime;
/// <summary>
/// We want to teleport the right GameObject.
/// </summary>
public GameObject player;
public Transform[] otherTeleportations;
private void Awake()
{
// Checks if there is already a singleton in the scene and removes itself.
if (Instance == null) { Instance = this; }
else { Destroy(gameObject); }
}
}
Het TeleporterScript wordt op de teleportatiepads geplaatst en regelt de interactie met de speler, inclusief de input en de cooldown-logica.
public class TeleporterScript : MonoBehaviour
{
/// <summary>
/// This bool checks if the player is standing on the teleport.
/// </summary>
public bool isStandingOn = false;
/// <summary>
/// This is so you can say which number you want to teleport to.
/// </summary>
[SerializeField] private int teleportNumber;
private void Update()
{
// These sequence of checks is for to teleporter to work.
if (TeleportManager.Instance != null)
{
if (TeleportManager.Instance.isFueled && isStandingOn && !TeleportManager.Instance.isCooldown && Input.GetKeyDown(KeyCode.T))
{
TeleportPlayer(teleportNumber);
}
}
if (!TeleportManager.Instance.isFueled && Input.GetKeyDown(KeyCode.T))
{
TextManager.Instance.WhatToDisplay(TextManager.Instance.dialogueCell2[4]);
}
}
/// <summary>
/// This function is to teleport the player to the designated number given.
/// </summary>
/// <param name="numberToTeleport"></param>
private void TeleportPlayer(int numberToTeleport)
{
TeleportManager.Instance.player.GetComponent<CharacterController>().enabled = false;
TeleportManager.Instance.player.transform.position = TeleportManager.Instance.otherTeleportations[numberToTeleport].position;
StartCoroutine(Cooldown());
TeleportManager.Instance.player.GetComponent<CharacterController>().enabled = true;
}
/// <summary>
/// We don't want the player to infinitely keep teleporting so with this cooldown we stop that from happening.
/// </summary>
/// <returns></returns>
private IEnumerator Cooldown()
{
TeleportManager.Instance.isCooldown = true;
yield return new WaitForSeconds(TeleportManager.Instance.cooldownTime);
TeleportManager.Instance.isCooldown = false;
}
private void OnCollisionEnter(Collision other)
{
// For this check it is to see if the teleporter is getting fueled.
if (other.gameObject.CompareTag("FuelCell"))
{
TeleportManager.Instance.isFueled = true;
Destroy(other.gameObject);
}
}
}