繁体   English   中英

Coroutine 仅在我第一次渲染时运行 object (unity)

[英]Coroutine is only running for my first rendered object (unity)

我的协程只为第一个渲染的红色立方体触发一次。 我的节拍 map 中的其他节拍得到渲染,但不会移动到 (5,5) 所需的 position。 我错过了什么吗? 提前致谢!

我尝试添加一个 while 循环,但这似乎并没有解决问题。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class BeatMapConductor : MonoBehaviour
{
    private CSVReader beatmap;
    private MusicConductor music;

    private Vector3 redEndPosition = new Vector2(5,5);
    private Vector3 redStartPosition;
    private float desirecDuration = 5f;
    private float elapsedTime;
    private int i;

    private Queue<UnityEngine.GameObject> queue;
    private UnityEngine.GameObject[] array;


    // initializes variables before game starts
    void Awake()
    {
        beatmap = GetComponent<CSVReader>();
        music = GetComponent<MusicConductor>();
    }

    // Start is called before the first frame update
    void Start()
    {
        i = 0;
    }

    // Update is called once per frame
    void Update()
    {
        
        int roundedBeat = (int)Math.Round(music.songPositionInBeats, 0);

        if(i < beatmap.myPlayerList.player.Length && roundedBeat == beatmap.myPlayerList.player[i].beat){
            //rendering a new cube
            // Create a new cube primitive to set the color on
            GameObject redCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            // Get the Renderer component from the new cube
            var cubeRenderer = redCube.GetComponent<Renderer>();
            // Call SetColor using the shader property name "_Color" and setting the color to red
            cubeRenderer.material.SetColor("_Color", Color.red);
            
            i++;
            StartCoroutine(Movement(redCube));
        }

         
    } 

    IEnumerator Movement(GameObject cube){
        // to move the game object
        redStartPosition = cube.transform.position;
        while(elapsedTime < desirecDuration){
            elapsedTime += Time.deltaTime;
            float percentageComplete = elapsedTime / desirecDuration;
            cube.transform.position = Vector2.Lerp(redStartPosition, redEndPosition, percentageComplete);
        }
        yield return null;
    }
}

当您第一次运行Movement时,它会将elapsedTime增加到至少 5。当您现在第二次运行Movement时, elapsedTime仍然至少为 5,因此while (elapsedTime < desirecDuration)将评估为false并且您的第二个立方体不会被移动。 您应该将elapsedTime协程的局部变量(以及redStartPosition )。 另外,您应该放置yield return null; while循环的末尾让立方体以帧为基础移动。 按照您编写它的方式,循环将在一帧中立即将elapsedTime增加到 5。

IEnumerator Movement(GameObject cube) {
    float elapsedTime = 0f; // make it local for every coroutine instance
    // to move the game object
    Vector3 redStartPosition = cube.transform.position;
    while (elapsedTime < desirecDuration) {
        elapsedTime += Time.deltaTime;
        float percentageComplete = elapsedTime / desirecDuration;
        cube.transform.position = Vector2.Lerp(redStartPosition, redEndPosition, percentageComplete);
        yield return null; // on loop run per frame
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM