game: added additional walkpoints

This commit is contained in:
kookiekenobi 2025-01-24 23:36:16 +08:00
parent 442fae7644
commit d580f52362

View file

@ -14,7 +14,9 @@ public class NPCMovement : MonoBehaviour
/// <summary> /// <summary>
/// References and Variables /// References and Variables
/// </summary> /// </summary>
public Transform[] walkPoints; public Transform[] walkPoints1;
public Transform[] walkPoints2;
public Transform[] walkPoints3;
public float movementSpeed = 10.0f; public float movementSpeed = 10.0f;
public float turnSpeed = 5.0f; public float turnSpeed = 5.0f;
private int currentPtIndex = 0; private int currentPtIndex = 0;
@ -24,23 +26,36 @@ public class NPCMovement : MonoBehaviour
/// </summary> /// </summary>
void Start() void Start()
{ {
StartCoroutine(Walking()); StartCoroutine(WalkingToPlayer());
}
///<summary>
///
/// </summary>
public void WalkToPlayerLeft()
{
StartCoroutine(WalkingToPlayerLeft());
}
public void WalkToPlayerRight()
{
StartCoroutine(WalkingToPlayerRight());
} }
/// <summary> /// <summary>
/// Coroutine for NPC to walk to each point /// Coroutine for NPC to walk to each point
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
IEnumerator Walking() IEnumerator WalkingToPlayer()
{ {
while (currentPtIndex < walkPoints.Length) while (currentPtIndex < walkPoints1.Length)
{ {
if (walkPoints.Length == 0) if (walkPoints1.Length == 0)
{ {
yield break; yield break;
} }
Transform targetPt = walkPoints[currentPtIndex]; Transform targetPt = walkPoints1[currentPtIndex];
Vector3 direction = targetPt.position - transform.position; Vector3 direction = targetPt.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x,0,direction.z)); Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x,0,direction.z));
@ -53,7 +68,81 @@ public class NPCMovement : MonoBehaviour
/*Debug.Log($"Reached {currentPtIndex}");*/ /*Debug.Log($"Reached {currentPtIndex}");*/
currentPtIndex++; currentPtIndex++;
if (currentPtIndex >= walkPoints.Length) if (currentPtIndex >= walkPoints1.Length)
{
StopAllCoroutines();
yield break;
}
}
yield return null;
}
}
/// <summary>
/// Coroutine for NPC to walk to each point
/// </summary>
/// <returns></returns>
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;
}
}
/// <summary>
/// Coroutine for NPC to walk to each point
/// </summary>
/// <returns></returns>
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(); StopAllCoroutines();
yield break; yield break;