From 0ff78387dd584d2a27e9896ed72cd85e1791de9a Mon Sep 17 00:00:00 2001
From: Ghazi Akmal Fauzan <13521058@std.stei.itb.ac.id>
Date: Sat, 11 May 2024 11:40:43 +0700
Subject: [PATCH] fix: enemy patrol

---
 Assets/Prefab/Enemy/Bondowoso.prefab    |  6 +++---
 Assets/Prefab/Enemy/Jenderal.prefab     |  6 +++---
 Assets/Prefab/Enemy/KepalaKeroco.prefab |  4 ++--
 Assets/Prefab/Enemy/Keroco.prefab       |  6 +++---
 Assets/Scripts/Enemy/EnemyMovement.cs   | 20 +++++++++++---------
 5 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/Assets/Prefab/Enemy/Bondowoso.prefab b/Assets/Prefab/Enemy/Bondowoso.prefab
index 5e514a8..5df316c 100644
--- a/Assets/Prefab/Enemy/Bondowoso.prefab
+++ b/Assets/Prefab/Enemy/Bondowoso.prefab
@@ -114,7 +114,7 @@ NavMeshAgent:
   m_AutoRepath: 1
   m_Height: 7
   m_BaseOffset: 0
-  m_WalkableMask: 5
+  m_WalkableMask: 1
   m_ObstacleAvoidanceType: 4
 --- !u!114 &-1231610468905177093
 MonoBehaviour:
@@ -151,8 +151,8 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   visionRadius: 100
   attackRadius: 20
-  patrolRadius: 20
-  patrolDistanceLimit: 10
+  patrolRadius: 25
+  patrolDistanceLimit: 20
   rotationSpeed: 5
   attackCooldownTime: 3
   enemyAnimator: {fileID: 1029985441028286264}
diff --git a/Assets/Prefab/Enemy/Jenderal.prefab b/Assets/Prefab/Enemy/Jenderal.prefab
index 369ce1e..6dafa7f 100644
--- a/Assets/Prefab/Enemy/Jenderal.prefab
+++ b/Assets/Prefab/Enemy/Jenderal.prefab
@@ -111,7 +111,7 @@ NavMeshAgent:
   m_AutoRepath: 1
   m_Height: 5
   m_BaseOffset: 0
-  m_WalkableMask: 4294967295
+  m_WalkableMask: 1
   m_ObstacleAvoidanceType: 4
 --- !u!114 &4709834490000420793
 MonoBehaviour:
@@ -148,8 +148,8 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   visionRadius: 75
   attackRadius: 5
-  patrolRadius: 15
-  patrolDistanceLimit: 8
+  patrolRadius: 25
+  patrolDistanceLimit: 20
   rotationSpeed: 5
   attackCooldownTime: 2
   enemyAnimator: {fileID: 7283786664019546727}
diff --git a/Assets/Prefab/Enemy/KepalaKeroco.prefab b/Assets/Prefab/Enemy/KepalaKeroco.prefab
index 08f5461..0ed4815 100644
--- a/Assets/Prefab/Enemy/KepalaKeroco.prefab
+++ b/Assets/Prefab/Enemy/KepalaKeroco.prefab
@@ -127,7 +127,7 @@ MonoBehaviour:
   visionRadius: 50
   attackRadius: 15
   patrolRadius: 20
-  patrolDistanceLimit: 10
+  patrolDistanceLimit: 15
   rotationSpeed: 5
   attackCooldownTime: 3
   enemyAnimator: {fileID: 4075058093763895821}
@@ -152,7 +152,7 @@ NavMeshAgent:
   m_AutoRepath: 1
   m_Height: 3.68
   m_BaseOffset: 0
-  m_WalkableMask: 4294967295
+  m_WalkableMask: 1
   m_ObstacleAvoidanceType: 4
 --- !u!82 &-6884356819932166858
 AudioSource:
diff --git a/Assets/Prefab/Enemy/Keroco.prefab b/Assets/Prefab/Enemy/Keroco.prefab
index 6f7c64e..f279d74 100644
--- a/Assets/Prefab/Enemy/Keroco.prefab
+++ b/Assets/Prefab/Enemy/Keroco.prefab
@@ -108,7 +108,7 @@ NavMeshAgent:
   m_AutoRepath: 1
   m_Height: 3.5
   m_BaseOffset: 0
-  m_WalkableMask: 4294967295
+  m_WalkableMask: 1
   m_ObstacleAvoidanceType: 4
 --- !u!114 &2406453993655408128
 MonoBehaviour:
@@ -145,8 +145,8 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   visionRadius: 50
   attackRadius: 4
-  patrolRadius: 10
-  patrolDistanceLimit: 5
+  patrolRadius: 15
+  patrolDistanceLimit: 10
   rotationSpeed: 5
   attackCooldownTime: 3
   enemyAnimator: {fileID: 3386647410158969942}
diff --git a/Assets/Scripts/Enemy/EnemyMovement.cs b/Assets/Scripts/Enemy/EnemyMovement.cs
index ce92ec3..26b1df5 100644
--- a/Assets/Scripts/Enemy/EnemyMovement.cs
+++ b/Assets/Scripts/Enemy/EnemyMovement.cs
@@ -16,14 +16,12 @@ public class EnemyMovement : MonoBehaviour
     private float lastAttackTime;
     private Transform player;
     private NavMeshAgent agent;
-    private Vector3 lastPatrolPosition;
     private AudioSource audioSource;
 
     void Awake()
     {
         player = GameObject.FindGameObjectWithTag("Player")?.transform;
         agent = GetComponent<NavMeshAgent>();
-        lastPatrolPosition = transform.position;
         agent.updateRotation = false;
         lastAttackTime = Time.time;
         audioSource = GetComponent<AudioSource>();
@@ -109,15 +107,19 @@ public class EnemyMovement : MonoBehaviour
         enemyAnimator.SetBool("isAttacking", false);
         enemyAnimator.SetBool("isChasing", false);
         enemyAnimator.SetBool("isPatrolling", true);
-        if (Vector3.Distance(transform.position, lastPatrolPosition) >= patrolDistanceLimit)
+
+        // Check if the enemy has reached the current patrol destination
+        if (!agent.pathPending && agent.remainingDistance < 0.5f)
         {
-            Vector2 randomDirection = Random.insideUnitCircle.normalized * patrolRadius;
-            Vector3 randomPos = transform.position + new Vector3(randomDirection.x, 0, randomDirection.y);
-            NavMeshHit hit;
-            if (NavMesh.SamplePosition(randomPos, out hit, patrolRadius, 1))
+            // Generate a new random patrol destination
+            Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
+            randomDirection += transform.position;
+
+            // Ensure the new destination is on the NavMesh
+            if (NavMesh.SamplePosition(randomDirection, out NavMeshHit hit, patrolRadius, 1))
             {
-                lastPatrolPosition = hit.position;
-                agent.SetDestination(lastPatrolPosition);
+                RotateTowards(hit.position);
+                agent.SetDestination(hit.position);
             }
         }
     }
-- 
GitLab