Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Trying to script jumping... Rigidbody.AddForce is random?

Discussion in 'Scripting' started by Khyrid, May 3, 2013.

  1. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    SOLVED: (See my second post)

    Rigidbody.AddForce does not add force to the value I set it as!

    So I'm trying to script a sidescroller platformer character without using the character controller. When I attempted to use the character controller everything got weird and the character would fly through solid objects. Yeah, um, no thanks.

    Everything works fine with my own script except the jumping. I have yet to find anything in Unity as incredibly difficult as programming basic jumping.

    I have been using "Rigidbody.AddForce" to jump, and it kind of works except every so many jumps it randomly causes my character to fly up very fast and high. For gravity I'm using the gravity from the rigid body.

    Here is my code

    Code (csharp):
    1.  
    2. private var grounded : boolean = false;
    3. public var jumpVelocity : Vector3;
    4. private var jumping : boolean = false;
    5.  
    6. function Start () {
    7. jumpVelocity = Vector3(0, 5, 0);
    8. }
    9.  
    10. function Update () {
    11.    
    12. //Jump
    13. if(grounded  (Input.GetButtonDown("Jump") || Input.GetButton("Jump"))  jumping == false){ //grounded checks out correct
    14.         //if(rigidbody.velocity.y == 0){rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);} //<Tried this
    15.         //rigidbody.AddForce(jumpVelocity, ForceMode.Impulse); //<Tried this
    16.         rigidbody.AddForce (0, 100, 0);
    17.         jumping= true;
    18.         }
    19.  
    20. //Rest of script....
    21.  
    Is there any alternative to "Rigidbody.AddForce" that doesn't require me to use the character controller? I would like my character to jump to the same height every time I jump, not random heights each time.
     
    Last edited: May 4, 2013
    Igor_Vasiak likes this.
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    For jumping I think its better to set the velocity directly. Forces are hard to get right from memory.

    I think something like this should work..

    rigidbody.velocity = rigidbody.velocity + vector3(0,10,0)
     
  3. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    I tried it, but it didn't work. The character still jumps really high randomly every few jumps.

    I set the vector to (0,1,0) and I print the vector, it shows the rigidbody.velocity on the Y axis to be between around 2 and 3 and sometimes jumps to 6 or so. Come on Unity!

    ***EDIT***


    Solution:

    I tried

    Code (csharp):
    1. rigidbody.velocity = Vector3(0,8,0);
    And while it still fluctuates a little, it does not appear to have the problem of sometimes blasting my character off into oblivion when jumping. I'm tired and don't have time to thoroughly test it, but I think you were onto the right idea. Thanks.

    ....I tested it a lot today and it works solid!
     
    Last edited: May 4, 2013
  4. jingyiwang

    jingyiwang

    Joined:
    May 4, 2013
    Posts:
    2
    use fixedUpdate when dealing with rigidbodys. should fix your problem with different jumpHeights.
     
  5. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    I'll check that out.
     
  6. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,196
    One more thing you could try in case the other solutions don't solve your issue. Use ForceMode.Impulse as the second argument to the AddForce method. After all, if you're trying to mimicking a jump, a single force is more realistic than a continuous one, and should give more consistent results.
     
  7. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    If you look at some of the commented out code in my initial post, you will see I tried that. The rigidbody.velocity = Vector3(0,8,0); worked for me.
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Yeah I couldn't remember if I had set it directly as you ended up doing. The only thing that might be a problem with that code is it might stop any left or right movement, which is generally important for a platformer, but thats ok, you just add it into the velocity.

    Also, definitely put anything rigidbody related into fixedupdate.

    heh, just decided to go confirm you could read key presses from fixedupdate, and it seems setting the velocity is the Unity standard way for doing a jump...

    http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html
     
    Last edited: May 4, 2013
  9. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    It seems I began with the most obscure methods and worked my way towards the obvious and simple method everyone else already uses.

    As for the jump hindering right or left movement, I use this for horizontal movement:

    transform.Translate(moveSpeed * Time.deltaTime, 0f, 0f);

    And it hasn't been a problem.
     
  10. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    I guess using Translate for left/right makes sense, because it's not an "impulse" force like a jump that gets dragged down by gravity.

    Some good clarifications on this thread about forces and motion. Very useful!
     
  11. Reevezy

    Reevezy

    Joined:
    May 7, 2013
    Posts:
    26
    I know this is solved but I would just like to add, you can remove || Input.GetButton("Jump")) jumping == false from your if statement and the jumping bool all together.

    I'm pretty sure Input.GetButtonDown() only returns the button once every press.
     
  12. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Yes, I have some redundant conditions for sure. I'm going to trim it down.
     
    Last edited: May 9, 2013