【问题标题】:Coroutine is only running for my first rendered object (unity)协程只为我的第一个渲染对象运行(统一)
【发布时间】:2023-02-07 23:52:55
【问题描述】:

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

我尝试添加一个 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;
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    当你第一次运行Movement时,它会将elapsedTime增加到至少5。当你第二次运行Movement时,elapsedTime仍然至少是5,因此while (elapsedTime &lt; desirecDuration)将评估为@987654326 @ 并且你的第二个立方体不会被移动。您应该使 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2013-02-17
      • 2019-01-14
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      相关资源
      最近更新 更多