我一整天都在尝试从point A to point B顺利,所以我尝试了Lerp, 朝着去 and 平滑阻尼但每次物体都会从A点消失并立即出现在B点!
我尝试了在互联网上找到的所有解决方案,但得到了相同的结果。
你能救我一命并帮我解决这个问题吗?
这是我尝试过的代码:
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);
和更多...
您需要使用 Lerp 不断更新位置。您可以使用协程来执行此操作,如下所示(假设起点和终点是已定义的位置):
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;
}
}
您可以使用以下方式调用此协程:
StartCoroutine(moveObject());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)