portal with camera transformation
This commit is contained in:
36
Assets/Escape Room Engine/Portal/Portal.cs
Normal file
36
Assets/Escape Room Engine/Portal/Portal.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user