平稳地将对象移动到目标Unity3D [英] Move Object to Destination Smoothly Unity3D
问题描述
我整天试图将对象从 A点移动到 B 点,所以我尝试了 Lerp , MoveTowards 和 SmoothDamp ,但是每次对象刚从A点消失并立即出现在B点!
I am trying the whole day to move an object from point A to point B smoothly, so I tried Lerp, MoveTowards and SmoothDamp but every time the object just disappear from point A and appear on point B instantly!
我尝试了在互联网上找到的所有解决方案,但是得到了相同的结果.
I tried every solution that I found in internet but I got the same result.
您能救我一命并帮助我解决这个问题吗?
Can you please save my life and help me to solve that?
这是我尝试过的代码:
transform.position = Vector3.SmoothDamp(transform.localPosition, Destination, ref velocity, Speed);
transform.position = Vector3.Lerp(transform.localPosition, Destination, Speed);
transform.position = Vector3.MoveTowards(transform.localPosition, Destination, Speed);
还有更多...
And more...
推荐答案
您需要使用Lerp不断更新位置.您可以按照以下步骤使用协程进行此操作(假设起点"和目的地"是已定义的位置):
You need to continuously update the position using Lerp. You could do this using a coroutine as follows (assuming Origin and Destination are defined positions):
public IEnumerator moveObject() {
float totalMovementTime = 5f; //the amount of time you want the movement to take
float currentMovementTime = 0f;//The amount of time that has passed
while (Vector3.Distance(transform.localPosition, Destination) > 0) {
currentMovementTime += Time.deltaTime;
transform.localPosition = Vector3.Lerp(Origin, Destination, currentMovementTime / totalMovementTime);
yield return null;
}
}
您可以通过以下方式将此协程称为:
You would call this coroutine with:
StartCoroutine(moveObject());
这篇关于平稳地将对象移动到目标Unity3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!