Untitled
unknown
csharp
a year ago
1.6 kB
6
Indexable
using UnityEngine;
public class EndlessHallway : MonoBehaviour
{
public Transform teleportPoint; // Point to teleport player back to
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Calculate the local offset
Vector3 localOffset = transform.InverseTransformPoint(other.transform.position);
// Calculate the relative rotation between the trigger and the teleport point
Quaternion relativeRotation = teleportPoint.rotation * Quaternion.Inverse(transform.rotation);
CharacterController cc = other.GetComponent<CharacterController>();
if (cc != null)
{
cc.enabled = false; // Disable to allow teleportation
// Calculate the new position with local offset applied
other.transform.position = teleportPoint.TransformPoint(localOffset);
// Apply the calculated rotation
other.transform.rotation = relativeRotation * other.transform.rotation;
cc.enabled = true; // Re-enable after teleporting
}
else
{
// Apply position and rotation without character controller
other.transform.position = teleportPoint.TransformPoint(localOffset);
other.transform.rotation = relativeRotation * other.transform.rotation;
}
Debug.Log("Player teleported to: " + other.transform.position);
}
}
}
Editor is loading...
Leave a Comment