Files
Crash-Course/Assets/Scripts/Game/RespawnOnTriggerEnter.cs

57 lines
1.8 KiB
C#
Raw Permalink Normal View History

2025-04-18 15:54:50 -04:00
using UnityEngine;
using Game;
using Music;
using Player;
2025-04-16 19:57:54 -04:00
namespace Game
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// This class handles respawning objects when they collide with a trigger tagged with a specific value.
/// </summary>
public class RespawnOnTriggerEnter : MonoBehaviour
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// The spawn point where the object will respawn.
/// </summary>
public Vector2 spawnPoint;
/// <summary>
/// If true, the spawn point is set to the object's initial position.
/// </summary>
public bool spawnPointIsInitialPosition = false;
/// <summary>
/// The tag of the trigger that causes the object to respawn.
/// </summary>
public string respawnTag;
/// <summary>
/// Sets the spawn point to the object's initial position if specified.
/// </summary>
private void Start()
{
2025-04-18 15:54:50 -04:00
if (spawnPointIsInitialPosition)
{
// Set the spawn point to the object's initial position
spawnPoint = transform.position;
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Handles collisions with triggers and applies damage to the object if it has a <see cref="Damageable"/> component.
/// </summary>
/// <param name="other">The collider of the object that entered the trigger.</param>
private void OnTriggerEnter2D(Collider2D other)
{
2025-04-18 15:54:50 -04:00
// Check if the collider has the specified tag
if (other.CompareTag(respawnTag))
2025-03-07 10:03:16 -05:00
{
2025-04-18 15:54:50 -04:00
// Apply damage to the object if it has a Damageable component
if (TryGetComponent(out Damageable damageable))
{
damageable.Damage(9999f);
}
2025-03-07 10:03:16 -05:00
}
}
}
2025-04-18 15:54:50 -04:00
}