C# ';更平滑';用矢量3.Lerp看隐形传态
简短版本:我可以在C# ';更平滑';用矢量3.Lerp看隐形传态,c#,unity3d,C#,Unity3d,简短版本:我可以在transform.Translate(新向量3(-6,0))中使用Vector3.Lerp(不计算运动中的速度变量) 我有一个非常简单的游戏,玩家可以从一条车道移动到另一条车道(最多3条车道(比如庙宇跑))。现在我的角色正在传送到其他通道,我想用Vector3.Lerp使它看起来更平滑。我现在的问题是,我不知道如何在代码中实现它。我不使用速度变量计算我的移动,因为我的角色开始移动不均匀的距离,这是我不想要的。我有3条固定车道要移动 if (Input.GetKeyDown("
transform.Translate(新向量3(-6,0))中使用Vector3.Lerp
代码>(不计算运动中的速度变量)
我有一个非常简单的游戏,玩家可以从一条车道移动到另一条车道(最多3条车道(比如庙宇跑))。现在我的角色正在传送到其他通道,我想用Vector3.Lerp
使它看起来更平滑。我现在的问题是,我不知道如何在代码中实现它。我不使用速度变量计算我的移动,因为我的角色开始移动不均匀的距离,这是我不想要的。我有3条固定车道要移动
if (Input.GetKeyDown("a"))
{
// Do I need a speed variable here?
transform.Translate(new Vector3(-6, 0));
}
如果代码不在Update()中,则可以使用startcroutine
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;
}
if(Input.GetKeyDown(“a”)){
改变=真实;
开始例行程序(变更车道(int n));
}
IEnumerator换车道(int n){
float changeProgress=0f;
浮动时间=1f;
原始位置=转换位置;
//速度应为运行速度
targetPosition=初始位置+新矢量3(n*车道宽度、速度*变化持续时间);
while(变更进度<变更持续时间){
tranform.position=Vector3.Lerp(初始位置、目标位置、变更进度/变更持续时间);
changeProgress+=Time.DeltaTime;
收益率为0;
}
tranform.position=目标位置;
改变=错误;
}
如果代码在Update()中,您也可以使用它,但在更改车道时,不应开始新的更改