Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Does AddForce increase force over time?

Discussion in 'Scripting' started by Wrenger, Jun 10, 2020.

  1. Wrenger

    Wrenger

    Joined:
    Apr 28, 2020
    Posts:
    71
    I had a plane with a sphere on top. I used AddForce to move it in the update (*deltaTime) , but it seemed like it went faster with time.
    How do i make it stay the same speed?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    AddForce, as the name implied, adds a force to the object. This accelerates it into some direction. So by definition, adding a force is basically acceleration. Doing it each frame results in going faster and faster, just like if you kept the pedal pressed in a car. If you want to move with a constant speed there are quite a few options. Staying with the car analogy, you'll have to set a max speed, stop applying force once some velocity has been reached and reapplying it when you dip below that, or simply apply the correct amount of force to stay at some velocity. Some options that come to mind:
    • Manually set the position. This gives you full control over the speed, or even acceleration.
    • Put a cap on rigbidybody.velocity such that you allow acceleration but have some max speed.
    • Instead of a cap, stop applying a force once you reach some velocity, and reapply it once you fall below.
    • Directly set velocity. Keep in mind that this is not intended and generalls makes using physics a questionable choice in most situations.
    • Properly calculate the force you need to apply to reach some desired velocity. Dont just add the same force each frame. This would probably be the best option, but you'll have to do the math.
    • ... and whatever else you can think of
     
    Wrenger and PraetorBlue like this.
  3. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    Just as a complement to Yoreki's comment, everything he said is right, when you use AddForce, the rigidbody's speed increases with time, and this may even generate problems with colliders/triggers depending on how fast you are.

    The way I fixed this was by simply adding a "Constant Force" in the inspector for a certain object, that way the object's speed remained, well, constant throughout. Another alternative, as similar as Yoreki mentioned above is using Mathf.Clamp to limit the force you are applying on such rigidbody, which then limits your force between a minimim and a maximum value.

    I hope this helps.
     
    Wrenger likes this.
  4. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,098
    You should not use
    AddForce
    in
    Update
    and also do not multiply the force with
    deltaTime
    .

    Everything interacting with physics should be done in
    FixedUpdate
    . Many of the physics methods apply to physics timesteps and
    Update
    can be called multiple times or not at all for a given timestep.
    FixedUpdate
    , on the other hand, is called for every timestep.

    This is important because
    AddForce
    , depending on the force mode, already takes time into account. I.e.
    Force
    and
    Acceleration
    mean applying force/acceleration over the duration of the timestep the method was called in. Adding in another
    deltaTime
    , which might also reflect the
    Update
    delta and not the physics delta, will mess things up.

    (
    Impulse
    and
    VelocityChange
    , however, are intended for forces applied at once and not over a time period. It's therefore safe to call them outside of FixedUpdate but you still don't want to multiply with deltaTime.)
     
    halley, Sasune_Fair, Wrenger and 2 others like this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    2,001
    I've had fine results by directly setting velocity in Update:
    if(isActiveHyperDrive) g.GetComponent<Rigidbody>().velocity=Vector3.forward*10;
    keeps it at a constant speed (+10z/second). There's no need to worry about deltaTime or fixedDeltaTime, since velocity is in meters/sec -- the physics system handles movement per frame.

    In theory, FixedUpdate is a better spot than Update. If Updates occur more often then I'm wasting CPU time, and if FixedUpdate runs more often then your rigidbody might slow down a little before an Update resets the speed. But I've never noticed a problem. Even going from desktop to mobile the results are consistent.

    Using AddForce isn't exactly the same as changing velocity directly. AddForce has a delayed effect -- they seem to be "saved up" until the physics step applies them to the velocity. That might have some effect on certain collisions, but, again, I've never seen anything weird happen.
     
    Wrenger and busterlock like this.
  6. Wrenger

    Wrenger

    Joined:
    Apr 28, 2020
    Posts:
    71
    I feel like adding a drag of 1 on the rigidbody fixed it somehow.. That doesent make sense though. What is happening?
     
  7. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,098
    Drag results in a maximum velocity. The drag force increases with velocity, until it matches your added force and prevents the object from accelerating further. The higher the drag, the lower the maximum velocity will be.
     
    Mahandal likes this.
  8. Somnumbula

    Somnumbula

    Joined:
    Oct 2, 2023
    Posts:
    1
    I know this post is 4 years old, but still
    To anyone trying to understand how to resolve this issue, I've found that subtracting the rigidbody.velocity.magnitude from a speed value and multiplying the force vector by that achieves the constant moving speed effect
    Like this
    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     rb.AddForce(forceDirection * (speed - rb.velocity.magnitude));
    4. }
    You basically tell AddForce function here to adjust force for the desired speed, not just add it. And if you move an object or player like that the movement speed feels pretty consistent but still realistic
    Hope this helps!