Move Object to Destination Smoothly Unity3D

前端 未结 2 1107
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 18:06

I am trying the whole day to move an object from point A to point B smoothly, so I tried Lerp, MoveTowards

2条回答
  •  时光取名叫无心
    2021-01-16 18:44

    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());
    

提交回复
热议问题