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:
     
    guido-paglie likes this.
  2. Sarper-Soher

    Sarper-Soher

    Joined:
    Mar 10, 2009
    Posts:
    799
    Hi, hope I can clear some confusions.

    First of all the difference is, rigidbody.velocity changes the velocity in an instant while AddForce applies force on an updated basis based on how you use it.

    For example, in real world, when you kick the football, you apply a force to it but, it's a big force so ball's Z velocity suddenly becomes let's say 10. So from 0 it becomes 10 and it moves forward really fast, getting slower in time because of the air drag. Same applies for the drinks a barman tosses on the bar like in the movies.

    When you push a very heavy create, you apply force to it constantly because it's very heavy and it has a bigger mass so it resists the force therefore moves slower. And you add more force constantly to move it somewhere.

    When an attack helicopter shoots rockets, those rockets always move with the same speed because they have a constant pushing power behind them, or the space shuttles once they are taking off. So they don't lose speed. it's constant force.

    So about which works best for which situations as it should be already clear from the above examples,

    -Constant force for objects which have their own pushing power like rockets.

    -Rigidbody.velocity for objects which have a big starting power then getting slower by drag, like football, thrown hand grenade, arrow etc. (I've also used this for making a jump pad. So that when player triggers the jump pad, players Y velocity suddenly becomes 10 so it jumps up getting slower in time)

    -AddForce for adding forces constantly, moving objects with force every frame. If you know Marble Blast game, that's how it works. Based on the player input, you add a force to the marble. So as long as forward button is held down, a force of for example 5 added to it in that direction.

    Every physics based operation must be operated under FixedUpdate function. Because that's where Unity updates the physics on a fixed frame rate so the physics become more accurate. You don't have to worry about what happens in the low level. Just make sure you put them in that function or things become unstable.

    It doesn't mean it has to happen every frame once you put it under fixedupdate. Thrown objects usually work like this in pseudo code;

    If blabla key is pressed
    {
    Instantiate the thrown object in player hand position
    Make it's Z velocity something like 10
    }

    So it only happens when the key is pressed, so as soon as the key is pressed, the object gets cloned, and it's velocity being set, and it doesn't happen again until you press the key again. And when you press the key again a new object is cloned and it's velocity is being set...so you never update the same object every physics update.

    I hope it will help. Ask anything that's left unclear.
     
  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.
     
  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.
     
    Jeff425 and 40detectives like this.
  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:
    194
    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.
     
    dtq likes 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.
     
  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 UnitedVector 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. Sarper-Soher

    Sarper-Soher

    Joined:
    Mar 10, 2009
    Posts:
    799
    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!
     
unityunity