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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity 2d, addforce or adjust the rigidbody?

Discussion in 'Getting Started' started by Jnanda313, May 20, 2015.

  1. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    Hey so i've been noticing that the majority of people use addforce to get their character to jump as opposed to doing something like this:

    Code (csharp):
    1.  
    2. if (Input.GetKey(KeyCode.Space) && grounded)
    3.         {
    4.             rB.velocity = new Vector2 (rB.velocity.x, jumpHeight);
    5.         }
    6.  
    Is there a major difference? Is addforce better than the method i use?
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    It depends on the angle from which you approach the problem. When setting velocity directly, you ignore the mass of the object, which is the same as applying a force that is great enough to push its mass to reach a certain velocity. On the other hand, applying a force takes the object's mass into account, resulting in different velocities for different masses.

    From a technical standpoint, I can't comment about whether it makes any difference as far as PhysX is concerned.
     
    NomadKing likes this.
  3. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    Thanks i get it now. I find it easier to adjust the jumpspeed/height by changing the rigidbody.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Changing velocity directly will override any existing momentum.
     
    NomadKing likes this.