Search Unity

Floating Rigidbody2D

Discussion in '2D' started by DomDomDom, Mar 27, 2015.

  1. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Hello There,

    I upgraded my project to Unity5 this week and it seems that my Rigidbody2D now floats my GameObjects, in this case my ground enemy (the top hat velociraptor). To give an example, this is how it worked before and how it is currently working. I also tried recreating the entire GameObject and that seems to do other weird things. You can see that here. It seems that my ground variable is doing something weird.

    You can check out my full code for this GameObject here as well. I know the condition for jumping isn't perfect, its something I'm working on and I'm not sure how to get to work naturally. I also know my code is a little scattered, if anyone has tips on improving it I'll be happy to take advice.

    Here is my jump condition to help out.
    Code (csharp):
    1.  
    2. if (dir.y > 0 && grounded && anim.GetBool("Ground")){
    3.             // Add a vertical force to the enemy.
    4.             grounded = false;
    5.             anim.SetBool("Ground", false);
    6.             rb.AddForce(new Vector2(0f, jumpForce));
    7.         }
    8.  
    Thanks!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Probably a stupid question, but... Has the Gravity Scale on your RigidBody2D changed?
     
    Last edited: Mar 30, 2015
  3. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    No, I didn't change it at all when upgrading. :/ meh.

    I have it set to the same as my player, this is it below:
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    If you have an Animator component then turn-off the 'Apply Root Motion' perhaps.
     
  5. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    I have it turned off and get the same same result. I can upload my project to github if anyone would like to take a look at it that way.
     
  6. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Go ahead and upload it...
     
  7. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Thanks guys, I appreciate you both looking into it. Here is a link to the github page..
     
  8. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    I tried you code and for me the problem with floating was solved when I unchecked "Apply Root Motion" (as MelvMay suggested). The enemey sometimes jumped rather high, but it always returned to the ground.
     
  9. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    It seems that I overlooked that. My apologies. Thank you for your help!

    As far as the jumping, I don't have a great condition at the moment. Pretty much, if the enemy is grounded and his position is greater than 0 on the Y axis he jumps. Any tips on improving this?