Untitled

 avatar
unknown
plain_text
2 months ago
1.8 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 offset only for horizontal alignment
            Vector3 offset = new Vector3(
                other.transform.position.x - transform.position.x,
                0, // Ignore vertical offset for vertical alignment
                other.transform.position.z - transform.position.z
            );

            Debug.Log("Offset from Trigger to Player (Horizontal): " + offset);

            // Handle teleportation
            CharacterController cc = other.GetComponent<CharacterController>();
            if (cc != null)
            {
                cc.enabled = false; // Disable to allow teleportation
                other.transform.position = teleportPoint.position + offset; // Apply horizontal offset
                cc.enabled = true; // Re-enable after teleporting
            }
            else
            {
                other.transform.position = teleportPoint.position + offset; // Apply horizontal offset
            }

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