Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Trouble understanding some code in Unity's 2D Platformer Character Controller video

Discussion in '2D' started by Juggerz, Apr 25, 2020.

  1. Juggerz

    Juggerz

    Joined:
    Apr 24, 2020
    Posts:
    3
    At 3:49 in this video, can someone explain why the velocity is multiplied by Time.deltaTime twice?
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Okay, this one is weird for a few reasons, but it looks correct... but I wouldn't do it this way as it is devious.
    First, using Time.deltatime instead of Time.fixeddeltatime. Supposedly unity makes these interchangable in the fixedupdate, but personally I find that a really devious thing to do.
    To your issue, Second, is what looks like time.deltatime being multiplied in twice improperly. To understand why this is actually not improper you have to realize what Physics2D.gravity is, it is an acceleration, not a raw velocity. Acceleration are actually over the time SQUARED, so to cancel that out properly to get a distance you do in fact have to multiple by time twice, not just once like you'd intuitively think.

    I perfectly understand the confusion, this one I had to hunt to figure out, but it does look right. If you're going to use it I would recommend some code comments :).
     
    Juggerz likes this.
  3. Juggerz

    Juggerz

    Joined:
    Apr 24, 2020
    Posts:
    3
    Thanks and I eventually managed to figure it out.

    Basically to get your current velocity at a certain point in time, you can do velocity = acceleration * total time elapsed
    So we can do velocity = gravity * Time.DeltaTime;
    However, since time.DeltaTime returns the time that has elapsed from the last frame and not the total time, we need to add the result of the calculation to our velocity instead of just assigning it.

    So:

    velocity += gravity * Time.DeltaTime;

    So now we have our current velocity at a certain point in time. We want to use this value to figure out our current position which is our displacement from the previous frame. And the physics formula to figure out displacement is displacement = velocity * Time elapsed. Since Time.deltaTime returns the time since the last frame, this works perfectly.

    So we can do: displacement = velocity * Time.deltaTime;

    Now we have our displacement from our last frame so all we need to is set our position to += our previous position.
     
  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Yeah. Honestly this is a rather iffy simulation of gravity since it uses discrete intervals. You'd probably actually want to use a continuous position calculation from the original position and such, but I think they were just putting that together as a demo of the idea, rather than a recommended approach. It isn't terrible by any stretch and for most any game, especially as most games have atypical gravity, would do just fine, but if you're were a hard core physicist who demands perfect realism you'd be crying foul.
     
  5. Juggerz

    Juggerz

    Joined:
    Apr 24, 2020
    Posts:
    3
    I didn't even take high school physics so I'm good lol. But I do want to learn physics so I can make my own 2d character controller.