52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
/// <summary>
|
|
/// Gate structure that can be destroyed.
|
|
/// Uses HealthComponent for health management.
|
|
/// </summary>
|
|
[RequireComponent(typeof(HealthComponent))]
|
|
public class Gate : MonoBehaviour
|
|
{
|
|
private HealthComponent _health;
|
|
private NavMeshObstacle _obstacle;
|
|
|
|
void Awake()
|
|
{
|
|
_health = GetComponent<HealthComponent>();
|
|
_obstacle = GetComponent<NavMeshObstacle>();
|
|
|
|
// Subscribe to health component events
|
|
_health.OnDeath += HandleDeath;
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (_health != null)
|
|
{
|
|
_health.OnDeath -= HandleDeath;
|
|
}
|
|
}
|
|
|
|
private void HandleDeath()
|
|
{
|
|
// Disable the gate visually
|
|
gameObject.SetActive(false);
|
|
|
|
// Disable NavMesh carving so enemies can pass through
|
|
if (_obstacle != null)
|
|
{
|
|
_obstacle.carving = false;
|
|
_obstacle.enabled = false;
|
|
}
|
|
|
|
// Destroy the object after a short delay
|
|
Destroy(gameObject, 0.1f);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the health component for direct access.
|
|
/// </summary>
|
|
public HealthComponent Health => _health;
|
|
}
|