using UnityEngine; using System; namespace Beyond { [Serializable] public class TeleportObject { [Tooltip("If custompossition is null and teleport is selected, the player is teleported to the camera position.")] public bool useTeleport; public GameObject playerObject; public Transform targetPosition; [Tooltip("Sets y rotation from camera to player, or if set, use customPosition rotation Y.")] public bool useCameraRotation; public bool detectGround; public LayerMask ground = 1; /// /// Teleport object do customPosition transform when targetPosition is null other wise teleport to customPosition. /// /// public void Teleport(Transform customPosition = null) { if (useTeleport && playerObject != null) { Vector3 teleportPosition = Vector3.zero; Quaternion teleportRotation = Quaternion.identity; float groundLevel = playerObject.transform.position.y; if (customPosition && targetPosition == null) { teleportPosition = customPosition.position; teleportRotation = customPosition.rotation; groundLevel = customPosition.position.y; } else if (targetPosition != null) { teleportPosition = targetPosition.transform.position; teleportRotation = targetPosition.transform.rotation; groundLevel = targetPosition.position.y; } if (detectGround) { Ray ray = new Ray(teleportPosition + Vector3.up, Vector3.down); if (Physics.Raycast(ray, out RaycastHit hit, float.MaxValue, ground, QueryTriggerInteraction.Ignore)) { groundLevel = hit.point.y; } } playerObject.transform.position = new Vector3(teleportPosition.x, groundLevel, teleportPosition.z); var rb = playerObject.GetComponent(); if (rb) { rb.position = playerObject.transform.position; rb.linearVelocity = Vector3.zero; } //rotate player if (useCameraRotation) { //cameraRotationStart = cameraRotationEnd; var playerRotation = playerObject.transform.rotation; playerRotation.y = teleportRotation.y; playerObject.transform.rotation = playerRotation; } } } } }