CoRoutine 没有正确重启
CoRoutine Not Restarting Properly
我有一些代码行应该将球 (Spark) 从一个位置移动到另一个位置,然后当它到达最后一个可用位置时,它会传送回起点。但是它似乎不起作用,它只是停在最后给出的位置。
这是代码:
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");
}
}
如果有人知道如何制作这项工作,我们将不胜感激。
yield return Movespark();
但是有一个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.
一个非常简单的替代方法是:
bool canMoveSpark = true;
void Update()
{
if (canMoveSpark) {
StartCoroutine(Movespark());
}
}
IEnumerator Movespark(){
canMoveSpark = false;
// Your code ...
canMoveSpark = true;
}
与问题无关,但你似乎有这个:
if(pointCount >= x){
pointCount = 0;
StartCoroutine("Movespark");
yield return null; // Waits a frame.
}
如果您希望 'exit' 脱离协程,请改用 yield break
。
// 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;
但是,出于任何原因您确实需要将它们分开,您可以改为这样做:
[SerializeField] private Transform[] points;
[SerializeField] private List<Transform> movePoints;
// ...
void Start(){
foreach (var point in points){
movePoints.Add(point);
}
}
// ...
我有一些代码行应该将球 (Spark) 从一个位置移动到另一个位置,然后当它到达最后一个可用位置时,它会传送回起点。但是它似乎不起作用,它只是停在最后给出的位置。 这是代码:
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");
}
}
如果有人知道如何制作这项工作,我们将不胜感激。
yield return Movespark();
但是有一个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.
一个非常简单的替代方法是:
bool canMoveSpark = true;
void Update()
{
if (canMoveSpark) {
StartCoroutine(Movespark());
}
}
IEnumerator Movespark(){
canMoveSpark = false;
// Your code ...
canMoveSpark = true;
}
与问题无关,但你似乎有这个:
if(pointCount >= x){
pointCount = 0;
StartCoroutine("Movespark");
yield return null; // Waits a frame.
}
如果您希望 'exit' 脱离协程,请改用 yield break
。
// 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;
但是,出于任何原因您确实需要将它们分开,您可以改为这样做:
[SerializeField] private Transform[] points;
[SerializeField] private List<Transform> movePoints;
// ...
void Start(){
foreach (var point in points){
movePoints.Add(point);
}
}
// ...