You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整Unity中StartCoroutine内线条绘制的速度?

How to Adjust Line Drawing Speed in Your Coroutine

Hey there! Let's break down how to tweak the speed of your line drawing in that StartCoroutine method. Looking at your code, right now the "speed" is tied directly to how fast your game runs through frames (since you're incrementing counter without any timing controls). Here are a few practical ways to make this speed controllable and consistent:

Option 1: Add Fixed Time Delays (Quick & Simple)

If you want a straightforward way to slow down or speed up the drawing, add a small wait between each step of your loop:

First, define a public variable at the top of your script so you can adjust it in the Unity Inspector:

public float drawStepDelay = 0.05f; // Smaller = faster drawing

Then update your moveLines coroutine to wait after each iteration:

IEnumerator moveLines() { 
    for (int i = 0; i < allLines.Count; i++) { 
        allLines[i].SetActive(true); 
        counter = 0; 
        endPos = allLines[i].GetComponent<EndHolder>().EndVector; 
        var dist = Vector3.Distance(endPos, startPos); 
        
        while (counter < 25) { 
            dist = Vector3.Distance(instancesToMove[0].transform.position, endPos); 
            counter++; 
            endPos = allLines[i].GetComponent<EndHolder>().EndVector; 
            startPos = allLines[...]; // Fill in your existing startPos logic
            
            // Add this line to control speed
            yield return new WaitForSeconds(drawStepDelay);
        }
    }
}

Tweak drawStepDelay to get the speed you want—lower values make the line draw faster, higher values slow it down.

For smoother, consistent speed that doesn't change if your game's frame rate drops, use Time.deltaTime to base movement on real time instead of frames. This is the more professional approach:

First, define a speed variable to control how fast the line moves per second:

public float lineMoveSpeed = 2f; // Units per second

Rewrite your coroutine to focus on distance instead of a counter:

IEnumerator moveLines() { 
    for (int i = 0; i < allLines.Count; i++) { 
        allLines[i].SetActive(true); 
        endPos = allLines[i].GetComponent<EndHolder>().EndVector; 
        startPos = instancesToMove[0].transform.position; // Make sure startPos is set correctly here
        
        // Use distance to check if we've reached the end (0.01f avoids floating point errors)
        while (Vector3.Distance(instancesToMove[0].transform.position, endPos) > 0.01f) { 
            // Calculate direction to the end point
            Vector3 moveDirection = (endPos - instancesToMove[0].transform.position).normalized;
            // Move the object a small amount based on time
            instancesToMove[0].transform.position += moveDirection * lineMoveSpeed * Time.deltaTime;
            
            // Wait for the next frame
            yield return null;
        }
    }
}

Now lineMoveSpeed controls how many units your line object moves every second—crank it up to make the line draw faster, lower it to slow it down. This works the same regardless of how many frames your game is running at.

Option 3: Adjust Counter Increment (If You Want to Keep Existing Logic)

If you prefer to stick with your counter system, you can make its increment rate dependent on time instead of just incrementing by 1 each frame:

Add a speed multiplier variable:

public float speedMultiplier = 1f; // Higher = faster counter growth

Update the counter increment in your loop:

while (counter < 25) { 
    dist = Vector3.Distance(instancesToMove[0].transform.position, endPos); 
    // Increment counter based on time (multiply by 10 to make adjustments more intuitive)
    counter += speedMultiplier * Time.deltaTime * 10; 
    endPos = allLines[i].GetComponent<EndHolder>().EndVector; 
    startPos = allLines[...]; // Your existing startPos logic
    
    yield return null;
}

This way, the counter grows at a consistent rate over time, so your line drawing speed stays steady even if frames are inconsistent.

内容的提问来源于stack exchange,提问作者Daniel Lip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:50:13