Can't get Time.deltaTime to add-up.

    void executeMovement() {
        if (done) return;
        prev = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        for (float i = 0.0000f; i < 1.0f; i += Time.deltaTime / 10) {
            transform.position = Vector3.Lerp(prev, MovingTo, i);
            Debug.Log("I: " + i + "| Time.deltaTime: " + Time.deltaTime);
            Debug.Break();
        }
        return;
    }

Is the code. On Debug.Break…

I: 0| Time.deltaTime: 0.02
to:
I: 0.6339989| Time.deltaTime: 0.02
up to:
I: 0.9999942| Time.deltaTime: 0.02

BEFORE, the Debug.Break(), that means that if user gets to see his/her first frame, object will be already at it’s location. Is there a way to let it execute that, but per each frame, instead of before first frame?

From what you’ve written, and looking at the code, I would imagine you’re trying to move an object to a target. However, the way you’re going about this issue is completely wrong.

IEnumerator Start()
{
   while(Vector3.Distance(transform.position, target) > margin)
   {
      transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
      yield return null;
   }
}

There are a bunch of ways to make something move to a target location. The above is just one example.

Basically, the script will run until the transform reaches the target. The margin value is at least how the close the transform has to be to the target before it stops moving. Speed is how quickly the transform will move toward it’s target. You will need to create variables for both. You could set margin to something like 0.1f and speed to 3f and then tweak the values to get your desired results.

I hope that helps and I would highly recommend that you look through the Unity API because there is a method/function for basically any problem that you may have.

1 Like

Here’s a pretty good tutorial to get you started with some concepts: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/coding-for-the-absolute-beginner

You can then move on to some of the platformer tutorials to see more character movement tips.

I know how Lerp works, 3 variables, 1# from, #2 to, #3 current progress.

“Coding for the absolute beginner”. Nice… I know how movement works, just this script doesn’t work out.

That’s not how Lerp is supposed to be used. Start position has to be the same, otherwise you’d need some more math to have a linear interpolation. The only parameter that has to be changed over time is the third one, but not the way you’ve done it :stuck_out_tongue:

1 Like

Sorry if you took offense. Just trying to be helpful. The problem is, that looking at your sample code, there are so many questions to answer, that it’s a better idea to watch one or two of the Unity tutorials which explain movement and all the steps leading to final code than to try and spell it out in text form here in the forum.

1 Like

You’re right, I forgot to change Vector3.Lerp to Vector3.MoveTowards. Anyways, code snippet has been edited.

1 Like