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

AddForce Not Working?

Discussion in 'Scripting' started by Shacxify, Jun 12, 2015.

  1. Shacxify

    Shacxify

    Joined:
    Jun 6, 2015
    Posts:
    24
    I'm pretty new to Unity, so I assume I jipped something up. I'm trying to use the AddForce function to simulate my object jumping. What am I doing wrong?
    Code (CSharp):
    1.  
    2.     void jump () {
    3.         if (Input.GetKey (KeyCode.W)) {
    4.             rb.AddForce(Vector2.up * thrust, ForceMode2D.Force);
    5.         }
    6.     }
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Snippet looks fine. When are you calling jump()?
     
  3. Shacxify

    Shacxify

    Joined:
    Jun 6, 2015
    Posts:
    24
    It's being called in the Update function, right after my movement function that controls left and right movement.
     
  4. j-bbr

    j-bbr

    Joined:
    Jun 10, 2015
    Posts:
    13
    What mass does your rigidbody have and what's the value of thrust?
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    This leads me to believe your thrust is too low. Yoy should also be doing physics in fixed update
     
  6. Shacxify

    Shacxify

    Joined:
    Jun 6, 2015
    Posts:
    24
    I fiddled around with it, editing to parts of movement code videos I was watching.
    Code (CSharp):
    1. //Jump Control
    2.     void jump () {
    3.         if (Input.GetKeyDown(KeyCode.W)) {
    4.             rb2D.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
    5.             Debug.Log("No.");
    6.         }
    7.     }