portal travelling
This commit is contained in:
38
Assets/Escape Room Engine/Portal/PortalDriver.cs
Normal file
38
Assets/Escape Room Engine/Portal/PortalDriver.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Escape_Room_Engine.Portal
|
||||
{
|
||||
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
|
||||
public class PortalDriver : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// The object that will be transported through the portal. Usually either this object or a parent offset object.
|
||||
/// </summary>
|
||||
[SerializeField] private Transform traveller;
|
||||
|
||||
/// <summary>
|
||||
/// The side of the portal this became tracked on.
|
||||
/// </summary>
|
||||
[HideInInspector] public int entrySide;
|
||||
|
||||
/// <summary>
|
||||
/// A reference to the collider of this portal driver.
|
||||
/// </summary>
|
||||
public Collider Collider { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// check whether the collider is set up correctly
|
||||
Collider = GetComponent<Collider>();
|
||||
if (Collider.isTrigger) throw new Exception("Collider must not be a trigger.");
|
||||
}
|
||||
|
||||
public void Teleport(Portal from, Portal to)
|
||||
{
|
||||
var m = to.transform.localToWorldMatrix * Portal.HalfRotation * from.transform.worldToLocalMatrix *
|
||||
traveller.localToWorldMatrix;
|
||||
traveller.transform.SetPositionAndRotation(m.GetPosition(), m.rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user