c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!!
Hi Everyone! I was trying to figure out how to make my character in Unity3D to patrol around different waypoints and at each of them, make a 10 second pause, i.e. stop at each waypoint. I tried using coroutines, but something doesn't go right. I am working with Unity 2018.4. Can somebody please help me!
Thanks in advanvce!
public class ActualWaypointsScript : MonoBehaviour
{
public List<Transform> waypoints = new List<Transform>();
private Transform targetWaypoint;
private int targetWaypointIndex = 0;
private float minDistance = 0.1f;
private int lastWaypointIndex;
private float movementSpeed = 5.0f;
private float rotationSpeed = 2.0f;
void Start()
{
targetWaypoint = waypoints[targetWaypointIndex];
lastWaypointIndex = waypoints.Count - 1;
}
void Update()
{
float movementStep = movementSpeed * Time.deltaTime;
float rotationStep = rotationSpeed * Time.deltaTime;
Vector3 directionToTarget = targetWaypoint.position - transform.position;
Quaternion rotationToTarget = Quaternion.LookRotation(directionToTarget);
transform.rotation = Quaternion.Slerp(transform.rotation, rotationToTarget, rotationStep);
Debug.DrawRay(transform.position, transform.forward * 50f, Color.green, 0f); //Draws a ray forward in the direction the enemy is facing
Debug.DrawRay(transform.position, directionToTarget, Color.red, 0f); //Draws a ray in the direction of the current target waypoint
float distance = Vector3.Distance(transform.position, targetWaypoint.position);
CheckDistanceToWaypoint(distance);
transform.position = Vector3.MoveTowards(transform.position, targetWaypoint.position, movementStep);
}
void CheckDistanceToWaypoint(float currentDistance)
{
if (currentDistance <= minDistance)
{
StartCoroutine(SetNextWaypointDelayed());
}
}
IEnumerator SetNextWaypointDelayed()
{
yield return new WaitForSeconds(5);
SetNextWaypoint();
}
void SetNextWaypoint()
{
targetWaypointIndex++;
UpdateTargetWaypoint();
}
void UpdateTargetWaypoint()
{
if (targetWaypointIndex > lastWaypointIndex)
{
targetWaypointIndex = 0;
}
targetWaypoint = waypoints[targetWaypointIndex];
}
}
Answer by goutham12 · Aug 27, 2019 at 04:18 AM
My Simple Aproch is
IEnumerator Move(List<Transform> wayPoints, Transform _player, float moveSpeed, float waitTime)
{
int index = 0;
float progress = 0.0f;
Vector3 startPos = _player.position;
Vector3 targetPos = wayPoints[index].position;
while (true)
{
progress += Time.deltaTime * moveSpeed;
_player.position = Vector3.Lerp(startPos, targetPos, progress);
yield return null;
if (progress >= 1.0f)
{
progress = 0.0f;
index += 1;
if (index >= wayPoints.Count - 1)
index = 0;
startPos = _player.position;
targetPos = wayPoints[index].position;
yield return new WaitForSeconds(waitTime);
}
}
}
Thank you for such a fast reply! Where should I insert this? And where should I paste the StartCoroutine ($$anonymous$$ove(...)); line?
Answer by Spy24 · Aug 27, 2019 at 10:23 AM
Thank you for such a fast reply! Where should I insert this? And where should I paste the StartCoroutine (Move(...)); line?
Call it on Start like void Start() { StartCoroutine($$anonymous$$ove(waypointslist,_playerTransform,the speed ,how much time it should wait to go to the next way point) }
void Start()
{
targetWaypoint = waypoints[targetWaypointIndex];
lastWaypointIndex = waypoints.Count - 1;
}
void Update()
{
float movementStep = movementSpeed * Time.deltaTime;
float rotationStep = rotationSpeed * Time.deltaTime;
Vector3 directionToTarget = targetWaypoint.position - transform.position;
Quaternion rotationToTarget = Quaternion.LookRotation(directionToTarget);
transform.rotation = Quaternion.Slerp(transform.rotation, rotationToTarget, rotationStep);
Debug.DrawRay(transform.position, transform.forward * 50f, Color.green, 0f); //Draws a ray forward in the direction the enemy is facing
Debug.DrawRay(transform.position, directionToTarget, Color.red, 0f); //Draws a ray in the direction of the current target waypoint
float distance = Vector3.Distance(transform.position, targetWaypoint.position);
CheckDistanceToWaypoint(distance);
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetWaypoint.position, movementStep);
}
void CheckDistanceToWaypoint(float currentDistance)
{
if (currentDistance <= $$anonymous$$Distance)
{
StartCoroutine($$anonymous$$ove(List<Transform> wayPoints, Transform _player, float moveSpeed, float waitTime));
targetWaypointIndex++;
UpdateTargetWaypoint();
}
}
IEnumerator $$anonymous$$ove(List<Transform> wayPoints, Transform _player, float moveSpeed, float waitTime)
{
int index = 0;
float progress = 0.0f;
Vector3 startPos = _player.position;
Vector3 targetPos = wayPoints[index].position;
while (true)
{
progress += Time.deltaTime * moveSpeed;
_player.position = Vector3.Lerp(startPos, targetPos, progress);
yield return null;
if (progress >= 1.0f)
{
progress = 0.0f;
index += 1;
if (index >= wayPoints.Count - 1)
index = 0;
startPos = _player.position;
targetPos = wayPoints[index].position;
yield return new WaitForSeconds(10);
}
}
}
void UpdateTargetWaypoint()
{
if (targetWaypointIndex > lastWaypointIndex)
{
targetWaypointIndex = 0;
}
targetWaypoint = waypoints[targetWaypointIndex];
}
}
The StartCoroutine($$anonymous$$ove(...)); is underlined red, for some reason it still doesn't really work. Sorry if I am annoying too much, but I am struggling with this for already five days.
here am attatching the whole script Just attatch the script to player. don't forget to remove your script that is attatched to the player.
fill the list in the player inspector with your waypoints.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class $$anonymous$$oveInPath : $$anonymous$$onoBehaviour { public List wayPoints; public float moveSpeed = 1.0f; public float waitTime = 0.5f; void Start() { StartCoroutine($$anonymous$$ove(wayPoints, transform, moveSpeed, waitTime)); } IEnumerator $$anonymous$$ove(List wayPoints, Transform _player, float moveSpeed, float waitTime) { int index = 0; float progress = 0.0f; Vector3 startPos = _player.position; Vector3 targetPos = wayPoints[index].position; while (true) { progress += Time.deltaTime * moveSpeed; _player.position = Vector3.Lerp(startPos, targetPos, progress); yield return null; if (progress >= 1.0f) { progress = 0.0f; index += 1; if (index >= wayPoints.Count - 1) index = 0; startPos = _player.position; targetPos = wayPoints[index].position; yield return new WaitForSeconds(waitTime); } } } }
Oh my God! It worked! Finally! You can't even imagine how grateful I am! Thank You so much!