Search Unity

AddForce vs. AddRelativeForce vs. rigidbody.velocity

Discussion in 'Scripting' started by tool55, Oct 26, 2009.

  1. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Hi guys,

    Noob question. I'm having a hard time getting a clear idea of which sort of force to apply to different projectiles.

    Which works best for:

    1. Thrown objects or bullets where there's an initial velocity or force and then physics takes over.

    2. Rocket type objects where there's a constant force and acceleration limited by drag.

    Also, in the scripting reference it suggests that physics based movements should be placed inside a FixedUpdate(). Does this apply to thrown objects? I mean, you wouldn't want to add a force on each cycle, would you? Wouldn't a function Start () work better? I'm confused.

    The scripting reference is a bit thin in this area. Thanks so much for the help.

    :roll:
     
    camdenvalkyrie and guido-paglie like this.
  2. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    802
    First of all the difference is, rigidbody.velocity sets the velocity property of the rigidbody while AddForce affects velocity while taking other factors into account (drag etc.) in a per-fixed-frame fashion (per FixedUpdate). A similar real world example would be letting a slingshot go vs pushing a heavy box. We don't directly set the velocity of objects in the real world but that's the closest example I can give.

    As for constant force, it's mainly for objects that have their own pushing power. Like rockets. It wouldn't be very different than doing AddForce in a script every FixedUpdate (without knowing the actual technical difference between the Constant Force component and doing it yourself, result would be the same nevertheless).

    Physics methods (like AddForce) should be called under FixedUpdate, because that is when Unity processes physics calls.You can set the velocity property whereever you want, since the set velocity will be used on the following fixed updates anyways.
     
    Last edited: May 19, 2021
    jmcusack, MoonJelly, Nefisto and 53 others like this.
  3. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Great explanation. Thank you so much for taking the time to write it out so clearly.

    My only other question would be with regard to AddRelativeForce. Am I correct to assume that this adds a force in local space? In other words, if you had a moving vehicle and shot a rocket from it, the force applied to the rocket would be the force added in scripting plus the force that the moving vehicle applies simply because it has forward velocity.

    Thanks again for your help. Thrown or fired objects are such a common part of games, I think your explanation will help a lot of us who are trying to figure all of this out.
     
    Mayonnaise2124 likes this.
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The direction of the force vector passed to AddRelativeForce is in local space, so if you passed Vector3.up, the force would be applied in the object's up direction, not the world's. The force applied is not relative to the motion of any other object - the "relative" part applies only to the direction of the force.
     
  5. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Got it. So it's relative direction, not force. I would assume it's use would still be primarily for objects in motion, such as cars, planes etc. where you want a force applied to a local axis of the vehicle. I think I've got it straight.

    Thanks so much for your guidance.
     
  6. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    195
    This is an old topic, to I hate to necro-bump it, but it helped me very much.

    Over the last few days, I have been scouring google + unity answers + forums to figure out how the ____ to get a proper rigidbody 3rd person type controller to work, and this was a nice breath of fresh air, to have the differences between these functions: AddRelativeForce, AddForce, and rigidbody.velocity spelled out so clearly.
     
    MoonJelly and dtq like this.
  7. Ryks

    Ryks

    Joined:
    Apr 11, 2014
    Posts:
    1
    I just had to say thank you to Sarper Soher, the clear explanation is a huge help. Apologies for 'necro-bump' or bad etiquette on old topic.

    I also wanted to add that there is a Constant Force component for your projectiles that makes them a snap to create, modify and use. (Not sure if in all versions I started with Unity4). This is for anyone else who finds this post in relation to projectiles.
     
    poiseh likes this.
  8. anonymousunitycreator

    anonymousunitycreator

    Joined:
    Sep 20, 2014
    Posts:
    43
    Shameless bump because this is one of the best explanations on the subject I have ever had the pleasure of reading.
     
  9. SpacePilot1000

    SpacePilot1000

    Joined:
    Dec 23, 2013
    Posts:
    7
    Sarper Soher's explanation was really helpful! Thank you. :)
     
  10. GoldenProlix

    GoldenProlix

    Joined:
    Aug 27, 2014
    Posts:
    1
    I would suggest not using rigidbody.velocity, it will interfere with the physics engine's calculations. Instead use
    Code (CSharp):
    1. rigidbody.AddForce(Velocity, Forcemode.VelocityChange)
    or if you want to have it take mass into consideration use
    Code (CSharp):
    1. rigidbody.AddForce(Velocity, ForceMode.Impulse)
     
    diliupg and vertig02 like this.
  11. wmdzbt

    wmdzbt

    Joined:
    Jan 1, 2016
    Posts:
    1
    感谢你的回答!解决了我的问题!我最近几小时一直在addforce和velocity之间纠结。
    But 1 thing I don't understand:
    Your code guide the way that a thing launched with a initial velocity, and after that frame, the thing will be in a physic world and by the rules it fell down or slow down or both.

    But if that's a rocket, it has a constant speed and direction, how can I code?
    If I addforce to it in every frame, I fare it will speed up because the energy is increasing,
    if I modify the thing's friction, that not the easy way and I don't like it.


     
  12. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    802
    http://docs.unity3d.com/Manual/class-ConstantForce.html

    The manual is your friend
     
    zero_null likes this.
  13. Chintan-Kansagara

    Chintan-Kansagara

    Joined:
    Jul 16, 2016
    Posts:
    19
    Hi friends
    main character to touch throw drag in screen next to two wall set but main character collision not working collide egnor and moves continus.
    plz help me.
     
  14. Iammnhamza

    Iammnhamza

    Joined:
    Jan 12, 2017
    Posts:
    3
  15. PanzerPotato

    PanzerPotato

    Joined:
    Oct 28, 2017
    Posts:
    21
    I know it's been a while, but here's an important note:
    1. If you use
      AddForce ()
      (non-relative) but in a relative direction (ie.
      transform.forward
      ), the force will still be applied in the relative direction.
    2. On the other hand, if you use
      AddRelativeForce ()
      and a relative direction, it will be treated as
      AddForce ()
      with a world-relative direction (kinda like how multiplying two negatives give a positive).
    Cheers!
     
    wm-VR, tomatohorse and DevLinkGames like this.
unityunity