Hi Guys,
it’s me again, your local idiot. I have a script to make an Object move along a path of nodes. The script works but I want the Object to pause at a certain node (GameObject stationNode). I googled a bit and found this => Yield Return waitforseconds(). I tried to implement it like I share in the screen below. But it’s not working at all.
The method IEnumerator WaitAtStation() gets called even when not at the stationNode. Also even tho it gets called there is no waiting time. I tried 10, I tried 10000 and then i started adding 10f 100000f. But whatever time I add in WaitForSeconds() it doesnt affect anything.
What am I doing wrong and how could i fix this thing?
using System.Collections;
using UnityEngine;
public class PathFollower : MonoBehaviour
{
Node[] PathNode;
public GameObject MovingObject;
[SerializeField] GameObject stationNode;
public float MoveSpeed;
float Timer;
//holds current node position
[SerializeField] int CurrentNode;
// static
[SerializeField] public static Vector3 CurrentPositionHolder;
void Start()
{
//Get Node children (from hierarchy)
PathNode = GetComponentsInChildren<Node>();
CheckNode();
MoveSpeed = 0.05f;
}
//function to check current node an move to it,
//by saving the node position to CurrentPositionHolder
void CheckNode()
{
if (CurrentNode < PathNode.Length)
{
Timer = 0;
// CurrentNode Position is put in CurrentPositionHolder
CurrentPositionHolder = PathNode[CurrentNode].transform.position;
//Debug.Log(PathNode);
}
//Looping back
if (CurrentNode == PathNode.Length -1)
{
CurrentNode = -1;
}
else if (MovingObject.transform.position == stationNode.transform.position)
Debug.Log("AtStationNode");
{
StartCoroutine(WaitAtStation());
Debug.Log("WaitAtStation executed");
}
}
void Update()
{
// This makes the thing move
Timer += Time.deltaTime * MoveSpeed;
if(MovingObject.transform.position != CurrentPositionHolder)
{
// If MovingObject not at node, move to node
MovingObject.transform.position =
Vector3.Lerp(MovingObject.transform.position, CurrentPositionHolder, Timer);
}
else
{
if (CurrentNode < PathNode.Length - 1)
{
// If MovingObject is at Node, move to next
CurrentNode++;
CheckNode();
}
}
}
IEnumerator WaitAtStation()
{
Debug.Log("WaitAtStation called");
yield return new WaitForSeconds(1000f);
}
}