finished portal fundimentals
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Runtime.InteropServices;
|
||||
using Unity.XR.CoreUtils;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
|
||||
public class StencilPortal : MonoBehaviour
|
||||
{
|
||||
@@ -19,31 +20,27 @@ public class StencilPortal : MonoBehaviour
|
||||
|
||||
public bool _shouldTeleport;
|
||||
|
||||
public TeleportationProvider teleportationProvider; // Reference to TeleportationProvider
|
||||
|
||||
public static Vector3 TransformPositionBetweenPortals(StencilPortal sender, StencilPortal target, Vector3 position)
|
||||
{
|
||||
return
|
||||
target.normalInvisible.TransformPoint(
|
||||
sender.normalVisible.InverseTransformPoint(position));
|
||||
return target.normalInvisible.TransformPoint(sender.normalVisible.InverseTransformPoint(position));
|
||||
}
|
||||
|
||||
public static Quaternion TransformRotationBetweenPortals(StencilPortal sender, StencilPortal target, Quaternion rotation)
|
||||
{
|
||||
return
|
||||
target.normalInvisible.rotation *
|
||||
Quaternion.Inverse(sender.normalVisible.rotation) *
|
||||
rotation;
|
||||
return target.normalInvisible.rotation * Quaternion.Inverse(sender.normalVisible.rotation) * rotation;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
//Debug.Log(transform.name + " player entered and should teleport");
|
||||
if (other.GetComponent<XROrigin>() == null) return;
|
||||
|
||||
if (occlusionPortal != null)
|
||||
{
|
||||
occlusionPortal.open = !occlusionPortal.open;
|
||||
}
|
||||
|
||||
if (!_shouldTeleport)
|
||||
{
|
||||
if (destroyAfterTeleport)
|
||||
@@ -56,31 +53,33 @@ public class StencilPortal : MonoBehaviour
|
||||
Debug.Log(transform.name + " player entered and should teleport");
|
||||
targetPortal._shouldTeleport = false;
|
||||
|
||||
// Move player to target portal collider relative position
|
||||
other.transform.position = TransformPositionBetweenPortals(this, targetPortal, other.transform.position);
|
||||
other.transform.rotation = TransformRotationBetweenPortals(this, targetPortal, other.transform.rotation);
|
||||
//rotatePlayer(other,targetPortal);
|
||||
if (teleportationProvider != null)
|
||||
{
|
||||
Vector3 targetPosition = TransformPositionBetweenPortals(this, targetPortal, other.transform.position);
|
||||
Quaternion targetRotation = TransformRotationBetweenPortals(this, targetPortal, other.transform.rotation);
|
||||
|
||||
TeleportRequest request = new TeleportRequest
|
||||
{
|
||||
destinationPosition = targetPosition,
|
||||
destinationRotation = targetRotation,
|
||||
matchOrientation = MatchOrientation.TargetUpAndForward
|
||||
};
|
||||
|
||||
teleportationProvider.QueueTeleportRequest(request);
|
||||
other.transform.rotation = TransformRotationBetweenPortals(this, targetPortal, other.transform.rotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("TeleportationProvider is not assigned!", this);
|
||||
other.transform.position = TransformPositionBetweenPortals(this, targetPortal, other.transform.position);
|
||||
other.transform.rotation = TransformRotationBetweenPortals(this, targetPortal, other.transform.rotation);
|
||||
}
|
||||
|
||||
if (destroyAfterTeleport)
|
||||
{
|
||||
Destroy(gameObject, 0.5f);
|
||||
}
|
||||
}
|
||||
private void rotatePlayer(Collider collider, StencilPortal target)
|
||||
{
|
||||
XROrigin Player = collider.GetComponent<XROrigin>();
|
||||
if (target != null && Player != null && Player.Camera != null)
|
||||
{
|
||||
// Calculate the rotation offset needed for the player
|
||||
Vector3 targetEulerAngles = target.transform.rotation.eulerAngles;
|
||||
Vector3 currentCameraEulerAngles = Player.Camera.transform.rotation.eulerAngles;
|
||||
|
||||
// Determine the rotation delta around the Y-axis
|
||||
float rotationDeltaY = targetEulerAngles.y - currentCameraEulerAngles.y - 180;
|
||||
|
||||
// Apply the rotation delta to the XR Origin
|
||||
Player.transform.Rotate(0, rotationDeltaY, 0, Space.World);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
@@ -97,8 +96,6 @@ public class StencilPortal : MonoBehaviour
|
||||
}
|
||||
|
||||
public StencilPortal TargetPortal => targetPortal;
|
||||
|
||||
public LayerMask DefaultLayer => defaultLayer;
|
||||
|
||||
public LayerMask PortalLayer => portalLayer;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user