From d580f523627f81e0d809f17ce7de097d8c76ca66 Mon Sep 17 00:00:00 2001 From: kookiekenobi Date: Fri, 24 Jan 2025 23:36:16 +0800 Subject: [PATCH] game: added additional walkpoints --- SSLR/Assets/Scripts/NPCMovement.cs | 103 +++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 7 deletions(-) diff --git a/SSLR/Assets/Scripts/NPCMovement.cs b/SSLR/Assets/Scripts/NPCMovement.cs index d389d31..dbd2201 100644 --- a/SSLR/Assets/Scripts/NPCMovement.cs +++ b/SSLR/Assets/Scripts/NPCMovement.cs @@ -14,7 +14,9 @@ public class NPCMovement : MonoBehaviour /// /// References and Variables /// - public Transform[] walkPoints; + public Transform[] walkPoints1; + public Transform[] walkPoints2; + public Transform[] walkPoints3; public float movementSpeed = 10.0f; public float turnSpeed = 5.0f; private int currentPtIndex = 0; @@ -24,23 +26,36 @@ public class NPCMovement : MonoBehaviour /// void Start() { - StartCoroutine(Walking()); + StartCoroutine(WalkingToPlayer()); + } + + /// + /// + /// + public void WalkToPlayerLeft() + { + StartCoroutine(WalkingToPlayerLeft()); + } + + public void WalkToPlayerRight() + { + StartCoroutine(WalkingToPlayerRight()); } /// /// Coroutine for NPC to walk to each point /// /// - IEnumerator Walking() + IEnumerator WalkingToPlayer() { - while (currentPtIndex < walkPoints.Length) + while (currentPtIndex < walkPoints1.Length) { - if (walkPoints.Length == 0) + if (walkPoints1.Length == 0) { yield break; } - Transform targetPt = walkPoints[currentPtIndex]; + Transform targetPt = walkPoints1[currentPtIndex]; Vector3 direction = targetPt.position - transform.position; Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x,0,direction.z)); @@ -53,7 +68,81 @@ public class NPCMovement : MonoBehaviour /*Debug.Log($"Reached {currentPtIndex}");*/ currentPtIndex++; - if (currentPtIndex >= walkPoints.Length) + if (currentPtIndex >= walkPoints1.Length) + { + StopAllCoroutines(); + yield break; + } + } + + yield return null; + } + } + + /// + /// Coroutine for NPC to walk to each point + /// + /// + IEnumerator WalkingToPlayerLeft() + { + while (currentPtIndex < walkPoints2.Length) + { + if (walkPoints2.Length == 0) + { + yield break; + } + + Transform targetPt = walkPoints2[currentPtIndex]; + + Vector3 direction = targetPt.position - transform.position; + Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x,0,direction.z)); + + transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime); + transform.position = Vector3.MoveTowards(transform.position, targetPt.position, movementSpeed * Time.deltaTime); + + if (Vector3.Distance(transform.position, targetPt.position) < 0.1f) + { + /*Debug.Log($"Reached {currentPtIndex}");*/ + currentPtIndex++; + + if (currentPtIndex >= walkPoints2.Length) + { + StopAllCoroutines(); + yield break; + } + } + + yield return null; + } + } + + /// + /// Coroutine for NPC to walk to each point + /// + /// + IEnumerator WalkingToPlayerRight() + { + while (currentPtIndex < walkPoints3.Length) + { + if (walkPoints3.Length == 0) + { + yield break; + } + + Transform targetPt = walkPoints3[currentPtIndex]; + + Vector3 direction = targetPt.position - transform.position; + Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x,0,direction.z)); + + transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime); + transform.position = Vector3.MoveTowards(transform.position, targetPt.position, movementSpeed * Time.deltaTime); + + if (Vector3.Distance(transform.position, targetPt.position) < 0.1f) + { + /*Debug.Log($"Reached {currentPtIndex}");*/ + currentPtIndex++; + + if (currentPtIndex >= walkPoints3.Length) { StopAllCoroutines(); yield break;