115 lines
3.5 KiB
C#
115 lines
3.5 KiB
C#
using UnityEngine;
|
|
using Polyart;
|
|
|
|
public class RespawnTrigger : MonoBehaviour
|
|
{
|
|
[Header("Respawn Settings")]
|
|
[SerializeField] private Transform respawnPoint;
|
|
[SerializeField] private bool usePlayerMiniGameStartPoint = true;
|
|
[SerializeField] private float respawnDelay = 0.5f;
|
|
|
|
[Header("Effects (Optional)")]
|
|
[SerializeField] private AudioClip respawnSound;
|
|
[SerializeField] private GameObject respawnEffect;
|
|
|
|
private AudioSource audioSource;
|
|
|
|
void Start()
|
|
{
|
|
// Get audio source if we have a respawn sound
|
|
if (respawnSound != null)
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
if (audioSource == null)
|
|
{
|
|
audioSource = gameObject.AddComponent<AudioSource>();
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnTriggerEnter(Collider other)
|
|
{
|
|
// Check if the player entered the trigger
|
|
FirstPersonController_Polyart player = other.GetComponent<FirstPersonController_Polyart>();
|
|
if (player != null)
|
|
{
|
|
StartCoroutine(RespawnPlayer(player));
|
|
}
|
|
}
|
|
|
|
System.Collections.IEnumerator RespawnPlayer(FirstPersonController_Polyart player)
|
|
{
|
|
// Play sound effect if available
|
|
if (respawnSound != null && audioSource != null)
|
|
{
|
|
audioSource.PlayOneShot(respawnSound);
|
|
}
|
|
|
|
// Wait for the specified delay
|
|
yield return new WaitForSeconds(respawnDelay);
|
|
|
|
// Determine respawn position
|
|
Vector3 targetPosition;
|
|
if (usePlayerMiniGameStartPoint && player.GetMiniGameStartPoint() != null)
|
|
{
|
|
targetPosition = player.GetMiniGameStartPoint().position;
|
|
}
|
|
else if (respawnPoint != null)
|
|
{
|
|
targetPosition = respawnPoint.position;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("No respawn point set! Using current position.");
|
|
targetPosition = transform.position + Vector3.up * 5f; // Default: 5 units above trigger
|
|
}
|
|
|
|
// Teleport the player
|
|
TeleportPlayer(player, targetPosition);
|
|
|
|
// Spawn effect at respawn location if available
|
|
if (respawnEffect != null)
|
|
{
|
|
Instantiate(respawnEffect, targetPosition, Quaternion.identity);
|
|
}
|
|
|
|
Debug.Log($"Player respawned at: {targetPosition}");
|
|
}
|
|
|
|
void TeleportPlayer(FirstPersonController_Polyart player, Vector3 position)
|
|
{
|
|
// Get the character controller
|
|
CharacterController characterController = player.GetComponent<CharacterController>();
|
|
|
|
if (characterController != null)
|
|
{
|
|
// Disable character controller temporarily for teleportation
|
|
characterController.enabled = false;
|
|
player.transform.position = position;
|
|
characterController.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
// Fallback if no character controller
|
|
player.transform.position = position;
|
|
}
|
|
}
|
|
|
|
// Optional: Visual indicator in the scene view
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
if (respawnPoint != null)
|
|
{
|
|
Gizmos.color = Color.green;
|
|
Gizmos.DrawWireSphere(respawnPoint.position, 1f);
|
|
Gizmos.DrawLine(transform.position, respawnPoint.position);
|
|
}
|
|
|
|
if (usePlayerMiniGameStartPoint)
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireCube(transform.position, transform.localScale);
|
|
}
|
|
}
|
|
}
|