I want to move the object slowly to the specified position.

Asked 1 years ago, Updated 1 years ago, 41 views

I want to move the object slowly to the specified position, but not slowly, but in an instant, I get to the last specified point.
Does Vector3.Lerp not work well with while?
Please let me know if there is a good way.

IEnumerator Move()
    {
        foreach(Transform_childobject in_pointParent)
        {
            _spped = 0;

            while(true)
            {

                _spped+=Time.deltaTime/2f;
                this.transform.position=Vector3.Lerp(this.transform.position,_childobject.position,_spped);

                if(_spped>1)break;

            }
             yield return null;
        }
    }

c# unity3d

2022-09-30 16:50

1 Answers

The col routine is aborted with yield return null; and resumed from the next frame.
Since the code provided is yield return null; after leaving the while statement, the while processing is processed in the same frame, and the processing is finished in an instant.Therefore, I think yield return null; should be written in the while statement.

The code presented seems to be intended to move through multiple points, so I will give you an example of the code based on that.

IEnumerator Move()
{
  foreach (Transform childobject in_pointParent)
  {
    var start = this.transform.position;
    var goal = childobject.position;
    start = 0.0f;

    while(t<1.0f)
    {
      t+ = Time.deltaTime/2.0f;
      t=(t>1.0f)?1.0f:t;// The upper limit is 1.0
      this.transform.position=Vector3.Lerp(start, goal, t);
      yield return null;
    }
  }
}


2022-09-30 16:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.