📜  unity 在 x 秒内移动到 pos - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:21.795000             🧑  作者: Mango

代码示例1
StartCoroutine(moveToPosition(yourTransform, newPosition, time));

public IEnumerator moveToPosition(Transform transform, Vector3 position, float timeToMove)
    {
        var currentPos = transform.position;
        var t = 0f;
        while (t < 1)
        {
            t += Time.deltaTime / timeToMove;
            transform.position = Vector3.Lerp(currentPos, position, t);
            yield return null;
        }
    }