Unity:如何使用Vector3.Lerp函数一个接一个地创建和扩展100个多维数据集

问题描述 投票:-1回答:1

我如何使用Vector3.Lerp函数一个接一个地创建和扩展100个多维数据集。我需要依赖持续时间变量。我需要一种有效的方法。

unity3d lerp
1个回答
0
投票

尽管您的问题含糊不清:

例如使用Coroutine,例如

// Adjust these in the Inspector
[SerilaizeField] private Vector3 targetScale = Vector3.one;
[SerilaizeField] private Vector3 startScale = Vector3.zero;
[SerilaizeField] private float durationPerCube = 1f;
[SerilaizeField] private Transform[] cubes;

private IEnumerator ScaleUpRoutine()
{
    if(cubes.Length == 0) yield break;

    foreach(var cube in cubes)
    {
        var timePassed = 0f;
        while(timePassed < durationPerCube)
        {
            var factor = timePassed / duration;
            // optional easing-in and -out
            //factor = Mathf.SmoothStep(0, 1, factor);

            cube.localScale = Vector3.Lerp(startScale, targetScale, factor);

            timePassed += Time.deltaTime;
            yield return null;
        }

        // just to be sure set a hard value
        cube.localScale = targetScale;
    }
}

您可以随时通过StartCoroutine启动它,例如

private void Start()
{
    StartCoroutine(ScaleUpRoutine);
}

如果您希望基于整体持续时间,可以这样做

// Adjust this in the Inspector
[SerilaizeField] private float overallDuration = 2f;

private IEnumerator ScaleUpRoutine()
{
    if(cubes.Length == 0) yield break;

    var durationPerCube = overallDuration / cubes.Length;

    ...

[请记住,尽管yield return null;当前每个立方体等待至少一帧,所以如果您的overallDuration非常小(<100帧),则可能不准确。在这种情况下,您可能需要添加StopWatch,以便仅在某个实时阈值之后才StopWatch帧,例如

yield return
© www.soinside.com 2019 - 2024. All rights reserved.