Untitled

 avatar
unknown
plain_text
2 months ago
1.6 kB
2
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"))
        {
            // 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 offset in world space between the player's position and the trigger's position
            Vector3 offset = other.transform.position - transform.position;
            Debug.Log("Offset from Trigger to Player: " + offset);

            // Handle teleportation
            CharacterController cc = other.GetComponent<CharacterController>();
            if (cc != null)
            {
                cc.enabled = false; // Disable to allow teleportation
                // Set the player's position directly to the teleportPoint, with the same offset in local space
                other.transform.position = teleportPoint.position + offset;
                cc.enabled = true; // Re-enable after teleporting
            }
            else
            {
                // No CharacterController, just teleport with the same offset
                other.transform.position = teleportPoint.position + offset;
            }


            // Log the new position after teleportation
            Debug.Log("Player teleported to: " + other.transform.position);
        }
    }
}
Leave a Comment