using UnityEngine; using UnityEngine.AI; /// /// Attach to any door/window barrier the zombie should break through. /// /// Setup: /// - Add a NavMeshObstacle component (Carve = true) so the NavMesh treats it as blocked. /// - Assign the barrier's layer to match AIBehaviour.barrierLayer. /// - Optionally populate the 'boards' array with child visuals that get hidden as health drops. /// public class Barrier : MonoBehaviour { [Header("Health")] public float maxHealth = 100f; [Header("Board Visuals (optional)")] [Tooltip("Child objects representing planks/boards. They are hidden progressively as health drops.")] public GameObject[] boards; public bool IsDestroyed { get; private set; } private float _health; private NavMeshObstacle _obstacle; void Awake() { _health = maxHealth; _obstacle = GetComponent(); } public void TakeDamage(float damage) { if (IsDestroyed) return; _health -= damage; UpdateBoards(); if (_health <= 0f) Break(); } void UpdateBoards() { if (boards == null || boards.Length == 0) return; // Show boards proportional to remaining health int boardsToShow = Mathf.CeilToInt((_health / maxHealth) * boards.Length); for (int i = 0; i < boards.Length; i++) { if (boards[i] != null) boards[i].SetActive(i < boardsToShow); } } void Break() { IsDestroyed = true; // Disable the NavMesh obstacle so pathfinding opens back up if (_obstacle != null) _obstacle.enabled = false; // Disable colliders so zombies walk through foreach (Collider col in GetComponentsInChildren()) col.enabled = false; // Hide visuals foreach (GameObject board in boards) if (board != null) board.SetActive(false); Destroy(gameObject, 0.1f); } #if UNITY_EDITOR void OnDrawGizmosSelected() { Gizmos.color = new Color(1f, 0.5f, 0f, 0.5f); Gizmos.DrawWireCube(transform.position, transform.localScale); } #endif }