diff --git a/Assets/Scenes/EnemyRope.unity b/Assets/Scenes/EnemyRope.unity index 2f55bd1..ba5d226 100644 --- a/Assets/Scenes/EnemyRope.unity +++ b/Assets/Scenes/EnemyRope.unity @@ -422,6 +422,7 @@ GameObject: - component: {fileID: 220016285} - component: {fileID: 220016286} - component: {fileID: 220016287} + - component: {fileID: 220016288} m_Layer: 0 m_Name: Enemy m_TagString: Untagged @@ -441,7 +442,13 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 6a3ffc4a3af9e0243ac9ee0c995bb82f, type: 3} m_Name: m_EditorClassIdentifier: - target: {fileID: 227211242} + targets: + - {fileID: 227211242} + - {fileID: 1259212314} + ropeDistCheck: 1 + ropeCheckMask: + serializedVersion: 2 + m_Bits: 64 --- !u!195 &220016282 NavMeshAgent: m_ObjectHideFlags: 0 @@ -566,7 +573,7 @@ CircleCollider2D: m_Material: {fileID: 0} m_IncludeLayers: serializedVersion: 2 - m_Bits: 0 + m_Bits: 4294967295 m_ExcludeLayers: serializedVersion: 2 m_Bits: 0 @@ -621,7 +628,34 @@ MonoBehaviour: m_PersistentCalls: m_Calls: [] minThreshold: 1 - squezeDamageScalor: 1 + squezeDamageScalor: 0 +--- !u!50 &220016288 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 220016280} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 0 + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 0 --- !u!114 &227211240 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 5467488512035376674, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} @@ -1458,6 +1492,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 4aabce12c1341ff488ee4dc806fd80e8, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!4 &1259212314 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 320690117028550720, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + m_PrefabInstance: {fileID: 1844950416} + m_PrefabAsset: {fileID: 0} --- !u!114 &1259212316 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 1749848915408613053, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} diff --git a/Assets/Scripts/Enemy/EnemyPathFinding.cs b/Assets/Scripts/Enemy/EnemyPathFinding.cs index 22acd78..55bf8e2 100644 --- a/Assets/Scripts/Enemy/EnemyPathFinding.cs +++ b/Assets/Scripts/Enemy/EnemyPathFinding.cs @@ -1,11 +1,15 @@ using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; using UnityEngine.AI; public class EnemyPathFinding : MonoBehaviour { - [SerializeField] private Transform target; + [SerializeField] private Transform[] targets; + + [SerializeField] private float ropeDistCheck = 1f; + [SerializeField] private LayerMask ropeCheckMask; NavMeshAgent agent; private void Start() @@ -16,6 +20,30 @@ public class EnemyPathFinding : MonoBehaviour } private void Update() { - agent.SetDestination(target.position); + 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; } }