37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Escape_Room_Engine.Portal
|
|
{
|
|
public class Portal : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// The portal that is connected with this one.
|
|
/// </summary>
|
|
public Portal other;
|
|
/// <summary>
|
|
/// The main camera to take the position for the portal camera from.
|
|
/// </summary>
|
|
[SerializeField] private Transform playerCamera;
|
|
/// <summary>
|
|
/// The camera that will draw the view for this portal.
|
|
/// </summary>
|
|
[SerializeField] private Transform portalCamera;
|
|
|
|
private void Start()
|
|
{
|
|
// check whether the other portal is set up
|
|
if (!other || other.other != this) throw new Exception("Other Portal not set up correctly.");
|
|
// check whether the player camera is set up
|
|
if (!playerCamera) throw new Exception("No camera initialised.");
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
var portalCameraMatrix = transform.localToWorldMatrix * other.transform.worldToLocalMatrix *
|
|
playerCamera.localToWorldMatrix;
|
|
portalCamera.SetPositionAndRotation(portalCameraMatrix.GetPosition(), portalCameraMatrix.rotation);
|
|
}
|
|
}
|
|
}
|