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. Dismiss Notice

Resolved Delta time doesn't work as supposed to

Discussion in 'Scripting' started by SunkenInTime, Jan 26, 2021.

  1. SunkenInTime

    SunkenInTime

    Joined:
    Dec 27, 2020
    Posts:
    12
    Code (CSharp):
    1.  void FixedUpdate()
    2.     {
    3.         //add a forward force
    4.         rigidBody.AddForce(fowardForce * Time.deltaTime, 0, 0 );
    5.  
    6.         //if (Input.GetKey("d"))
    7.         //{
    8.         //    rigidBody.AddForce(0,0,rightForce * Time.deltaTime);
    9.        // }
    10.     }
    This is the code, normally if I removed deltaTime it would work, but when I add it so it's not dependent on framerate, it doesn't move at all.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    SparrowGS and PraetorBlue like this.
  3. SunkenInTime

    SunkenInTime

    Joined:
    Dec 27, 2020
    Posts:
    12
    I'm basically trying to make sure the rate of movement isn't affected by framerate, this code is also from a tutorial I will link "
    " when he uses delta time, with the same values as me, it works for me. IDK if it's a unity update that changes that or something, I just want to be able to know how.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    That happens automatically whenever you set or change the velocity of a Rigidbody (e.g. via AddForce). The physics engine does the actual movement, and it is done in a framerate-independent way by default.
     
    SparrowGS likes this.
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,832
    FixedUpdate is already framerate-independent, so anything you do inside of FixedUpdate is already framerate-independent by default. Multiplying by Time.deltaTime just means it will be smaller by a factor of your fixed time step--which may sometimes be conceptually convenient so you can express things in amount-per-second instead of amount-per-frame, but won't change whether something is or is not framerate-dependent.

    (If you were using Update instead of FixedUpdate, then things would be different.)

    So when you say it "doesn't move at all", that's presumably just because you aren't pushing as hard. If you increase your "forwardForce" variable by a factor of 50, you should get the same result as before you started multiplying by Time.deltaTime. (By default, fixedDeltaTime = 0.02.)
     
    SparrowGS likes this.
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You missed the whole point of the name FixedUpdate, it means the time interval between calls is constant(in simulation time, not real life execution time, it gets a bit complicated)
     
  7. SunkenInTime

    SunkenInTime

    Joined:
    Dec 27, 2020
    Posts:
    12
    I tried increasing it by a value of 50, but nothing changed
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Did you remove Time.deltaTime, as it's not appropriate in this context?
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,832
    Then something is going on that I can't tell from the information you've posted. (Statistically, this means you are not testing what you think you are testing, because programmer mistakes are far more common than weird systemic problems.)
     
  10. SunkenInTime

    SunkenInTime

    Joined:
    Dec 27, 2020
    Posts:
    12
    nvm I'm stupid
     
  11. SunkenInTime

    SunkenInTime

    Joined:
    Dec 27, 2020
    Posts:
    12
    I made it a public variable, so it was being overridden by the values in the editor, so all I had to do was change it value in the editor.