Untitled

 avatar
unknown
csharp
2 months ago
1.7 kB
3
Indexable
using UnityEngine;

public class EndlessHallway : MonoBehaviour
{
    public Transform teleportPoint; // Point to teleport player back to
    public Vector3 teleportRotation = new Vector3(0, 0, 0); // Desired rotation after teleport (default facing forward)

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            // Log the player's world position
            Debug.Log("Player Position (World): " + other.transform.position);

            // Log the trigger's world position
            Debug.Log("Trigger Position (World): " + transform.position);

            // Calculate the local offset
            Vector3 localOffset = transform.InverseTransformPoint(other.transform.position);
            Debug.Log("Offset from Trigger to Player (Local): " + localOffset);

            other.transform.rotation = Quaternion.Euler(teleportRotation);
            // Handle teleportation
            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);
                
                cc.enabled = true; // Re-enable after teleporting
            }
            else
            {
                other.transform.position = teleportPoint.TransformPoint(localOffset); // Apply offset in world space
            }

            // Optionally adjust rotation
            Debug.Log("Player teleported to: " + other.transform.position);
        }
    }
}
Leave a Comment