【问题标题】:'Smoother' looking teleportation with Vector3.Lerp使用 Vector3.Lerp 进行“更平滑”的远程传输
【发布时间】:2015-06-29 17:05:33
【问题描述】:

短版:我可以在transform.Translate(new Vector3(-6,0)); 中使用Vector3.Lerp(不计算运动中的速度变量)吗?

我有一个非常简单的游戏,玩家可以从一条车道移动到另一条车道(最多 3 条车道(如 Temple Run))。现在我的角色只是传送到其他车道,我想用Vector3.Lerp让它看起来更流畅。我现在的问题是,我不知道如何在我的代码中实现它。我不使用速度变量来计算我的运动,因为我的角色开始移动不均匀的距离,这是我不想要的。我有 3 条固定车道要移动。

if (Input.GetKeyDown("a"))
{             
    // Do I need a speed variable here?
    transform.Translate(new Vector3(-6, 0));
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    如果代码不在Update()中,可以使用StartCoroutine

    if (Input.GetKeyDown("a")) {
        changing = true;
        StartCoroutine(ChangeLane(int n));
    }
    
    IEnumerator ChangeLane(int n) {
        float changeProgress = 0f;
        float changeDuration = 1f;
        originPosition = tranform.position;
        //the speed should be run speed
        targetPosition = originPosition + new Vector3(n * lanWidth, speed * changeDuration);
    
        while(changeProgress < changeDuration) {
            tranform.position = Vector3.Lerp(originPosition, targetPosition, changeProgress / changeDuration);
            changeProgress += Time.DeltaTime;
            yield return 0;
        }
        tranform.position = targetPosition;
        changing = false;
    }
    

    如果代码在Update()中,你也可以使用这个,但是在换道时,你不应该开始新的变化

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多