allow rigidbodies to travel through portals
This commit is contained in:
@@ -8,6 +8,7 @@ namespace Escape_Room_Engine.Portal
|
||||
{
|
||||
/// <summary>
|
||||
/// The object that will be transported through the portal. Usually either this object or a parent offset object.
|
||||
/// If left empty, it will default to this object.
|
||||
/// </summary>
|
||||
[SerializeField] private Transform traveller;
|
||||
|
||||
@@ -21,11 +22,19 @@ namespace Escape_Room_Engine.Portal
|
||||
/// </summary>
|
||||
public Collider Collider { get; private set; }
|
||||
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
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.");
|
||||
|
||||
// check whether the traveller is set
|
||||
if (!traveller) traveller = transform;
|
||||
|
||||
// get the rigidbody if there is one
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
public void Teleport(Portal from, Portal to)
|
||||
@@ -33,6 +42,11 @@ namespace Escape_Room_Engine.Portal
|
||||
var m = to.transform.localToWorldMatrix * Portal.HalfRotation * from.transform.worldToLocalMatrix *
|
||||
traveller.localToWorldMatrix;
|
||||
traveller.transform.SetPositionAndRotation(m.GetPosition(), m.rotation);
|
||||
if (_rigidbody)
|
||||
{
|
||||
_rigidbody.velocity = to.transform.TransformDirection(
|
||||
Portal.HalfRotation.rotation * from.transform.InverseTransformDirection(_rigidbody.velocity));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user