allow rigidbodies to travel through portals

This commit is contained in:
2022-10-10 15:54:32 +02:00
parent fcbef48483
commit 36e8538bb9
4 changed files with 430 additions and 483 deletions

View File

@@ -29,7 +29,7 @@ namespace Escape_Room_Engine.Portal
if (!GetComponent<Collider>().isTrigger) throw new Exception("Collider must be a trigger."); if (!GetComponent<Collider>().isTrigger) throw new Exception("Collider must be a trigger.");
} }
private void LateUpdate() private void FixedUpdate()
{ {
for (var i = 0; i < _closePortalDrivers.Count; i++) for (var i = 0; i < _closePortalDrivers.Count; i++)
{ {

View File

@@ -61,8 +61,8 @@ BoxCollider:
m_IsTrigger: 1 m_IsTrigger: 1
m_Enabled: 1 m_Enabled: 1
serializedVersion: 2 serializedVersion: 2
m_Size: {x: 1, y: 2, z: 0.1} m_Size: {x: 1, y: 2, z: 0.3}
m_Center: {x: 0, y: 1, z: -0.05} m_Center: {x: 0, y: 1, z: -0.15}
--- !u!1 &7398326895463990628 --- !u!1 &7398326895463990628
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -8,6 +8,7 @@ namespace Escape_Room_Engine.Portal
{ {
/// <summary> /// <summary>
/// The object that will be transported through the portal. Usually either this object or a parent offset object. /// 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> /// </summary>
[SerializeField] private Transform traveller; [SerializeField] private Transform traveller;
@@ -21,11 +22,19 @@ namespace Escape_Room_Engine.Portal
/// </summary> /// </summary>
public Collider Collider { get; private set; } public Collider Collider { get; private set; }
private Rigidbody _rigidbody;
private void Awake() private void Awake()
{ {
// check whether the collider is set up correctly // check whether the collider is set up correctly
Collider = GetComponent<Collider>(); Collider = GetComponent<Collider>();
if (Collider.isTrigger) throw new Exception("Collider must not be a trigger."); 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) 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 * var m = to.transform.localToWorldMatrix * Portal.HalfRotation * from.transform.worldToLocalMatrix *
traveller.localToWorldMatrix; traveller.localToWorldMatrix;
traveller.transform.SetPositionAndRotation(m.GetPosition(), m.rotation); traveller.transform.SetPositionAndRotation(m.GetPosition(), m.rotation);
if (_rigidbody)
{
_rigidbody.velocity = to.transform.TransformDirection(
Portal.HalfRotation.rotation * from.transform.InverseTransformDirection(_rigidbody.velocity));
}
} }
} }
} }

File diff suppressed because it is too large Load Diff