I have a some lines of code that are supposed to lerp a ball(Spark) from position to position and then when it reaches the last position available then it teleports back to the start. It doesn't seem to work however and it just stops at the last position given. This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
int pointCount;
public Transform point1, point2;
public float velocity;
float increment;
float time, incrementation;
[SerializeField] private Transform A,B,C,D,E,F,G,H,I,J;
[SerializeField] private List<Transform> movePoints;
[SerializeField] private Transform Spark;
// Start is called before the first frame update
void Start()
{
movePoints.Add(A);
movePoints.Add(B);
movePoints.Add(C);
movePoints.Add(D);
movePoints.Add(E);
movePoints.Add(F);
movePoints.Add(G);
movePoints.Add(H);
movePoints.Add(I);
movePoints.Add(J);
StartCoroutine("Movespark");
}
// Update is called once per frame
void Update()
{
Spark.position = Vector3.Lerp(point1.position, point2.position, incrementation);
incrementation = increment * Time.deltaTime;
}
IEnumerator Movespark(){
pointCount ;
var x = movePoints.Count;
incrementation = 0;
point1 = movePoints[pointCount];
if(pointCount < x){
point1 = movePoints[pointCount];
point2 = movePoints[pointCount 1];
}
if(pointCount >= x){
pointCount = 0;
StartCoroutine("Movespark");
yield return null;
}
float distance = Vector3.Distance(point1.position, point2.position);
increment = 1/(distance/velocity);
yield return new WaitUntil(() => point2.position.x <= Spark.position.x);
if(pointCount >= x)
pointCount = 0;
StartCoroutine("Movespark");
}
}
If anyone knows anyway of making this work it would be greatly appreciated.
CodePudding user response:
yield return Movespark();
But there is a Coroutine Limit.
Just so you are aware, there is a coroutine limit and if you hit this some coroutines will not fire or fail, leading to very weird and hard to track down behaviour if you have nested coroutines like this.
One very simple alternative is this:
bool canMoveSpark = true;
void Update()
{
if (canMoveSpark) {
StartCoroutine(Movespark());
}
}
IEnumerator Movespark(){
canMoveSpark = false;
// Your code ...
canMoveSpark = true;
}
Non question related, but it seems like you have this:
if(pointCount >= x){
pointCount = 0;
StartCoroutine("Movespark");
yield return null; // Waits a frame.
}
If you are looking to 'exit' out of the Coroutine, use yield break
instead.
// Seems Unnecessary
[SerializeField] private Transform A,B,C,D,E,F,G,H,I,J;
// Shows up in inspector, so just assign points 'A-J' here.
[SerializeField] private List<Transform> movePoints;
However, for any reason that you do need to separate them apart, you can do this instead:
[SerializeField] private Transform[] points;
[SerializeField] private List<Transform> movePoints;
// ...
void Start(){
foreach (var point in points){
movePoints.Add(point);
}
}
// ...