using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.AI; public class EnemyPathFinding : MonoBehaviour { [SerializeField] private Transform[] targets; [SerializeField] private float ropeDistCheck = 1f; [SerializeField] private LayerMask ropeCheckMask; NavMeshAgent agent; private void Start() { agent = GetComponent(); agent.updateRotation = false; agent.updateUpAxis = false; } private void Update() { Transform closestTarget = GetClosestTarget(); if (closestTarget == null) return; // Make sure no rope parts are in front Vector2 dir = closestTarget.position - transform.position; if (Physics2D.Raycast(transform.position, dir.normalized, ropeDistCheck, ropeCheckMask)) return; agent.SetDestination(closestTarget.position); } private Transform GetClosestTarget() { float dist = Mathf.Infinity; Transform shortest = targets[0]; foreach (var target in targets) { float targetDist = Vector2.Distance(target.position, transform.position); if (targetDist < dist) { dist = targetDist; shortest = target; } } return shortest; } }