Files
Inter-Face-Off/Assets/Scripts/World/PortalManager.cs
2026-01-15 18:18:59 +00:00

63 lines
2.2 KiB
C#

using UnityEngine;
namespace InterfaceOff.WorldScene
{
public class PortalManager : MonoBehaviour
{
[Header("References")]
[SerializeField] GameObject m_OtherPortal;
[SerializeField] Camera m_PlayerCamera;
[Header("Set References")]
[SerializeField] GameObject m_CameraPrefab;
[SerializeField] MeshRenderer[] m_Renderers;
[SerializeField] Transform m_PlayerPoint;
// Private variables //
PortalManager m_OtherManager;
[SerializeField] PortalCamera m_PortalCamera;
// Gets the other end of the portal
public PortalManager Linked() => m_OtherManager;
// Gets the location of the player relative to the portal
public Vector3 PlayerOffset() => m_PlayerPoint.localPosition;
// Start is called before the first frame update
void Start()
{
// Validates that it is a portal
m_OtherManager = m_OtherPortal.GetComponentInChildren<PortalManager>();
if (m_OtherManager == null)
{
Debug.LogError("OtherPortal was not valid portal");
return;
}
// Creates the camera in top-level heirachry and stores the PortalCamera script
GameObject cam = Instantiate(m_CameraPrefab, transform.root.parent);
m_PortalCamera = cam.GetComponentInChildren<PortalCamera>();
// Initialises the camera so it renders to the portal and not the screen
m_PortalCamera.InitCamera(m_Renderers, this, transform.parent.localEulerAngles * 2.0f);
}
void LateUpdate() => m_PlayerPoint.position = m_PlayerCamera.transform.position;
// When something enters the portal
private void OnTriggerEnter(Collider other)
{
// Changing the state if it is not the player will cause issues
if (!other.CompareTag("Player"))
{
return;
}
// Calculates if the player is going towards the portal
Vector3 difference = m_PlayerCamera.transform.position - transform.position;
PlayerController.SetPos(m_OtherManager.transform.position + difference - new Vector3(0, 1.0f, 0));
}
}
}