Search Unity

HELP!! RigidBody2D giving wrong? velocity values

Discussion in 'Editor & General Support' started by starwarsb2003, Oct 16, 2020.

  1. starwarsb2003

    starwarsb2003

    Joined:
    Sep 5, 2020
    Posts:
    8
    Whenever I add force to my character in the y direction to make him jump he doesn't move. Gravity and .moveposition works fine on the rigidbody2D, but velocity and .addForce does not. All help is appreciated!

    Character's just standing on the platform.
    The reason why the velocity is 246 is because I added a force to it, but it is clearly not moving the character.
    problem.PNG
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It may be an Animator or some other code overwriting the position of your object after the physics simulation.
     
  3. starwarsb2003

    starwarsb2003

    Joined:
    Sep 5, 2020
    Posts:
    8
    Same thing happens when I disable the animator and there's no other code that effects the physics on the object :(.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Maybe your character is stuck in the wall? From your screenshot that seems like maybe the case.
     
  5. starwarsb2003

    starwarsb2003

    Joined:
    Sep 5, 2020
    Posts:
    8
    No, I just happen to have the box collider not cover the players legs so make it seem like theyre on top of the object :/

    There's actually a bit of a gap in between them, this doesn't really mean anything though because I'm using raycasting and its detecting collision fine.

    Capture.PNG
     
  6. starwarsb2003

    starwarsb2003

    Joined:
    Sep 5, 2020
    Posts:
    8
    I have discovered some more things. Even though it says the velocity is zero, the relative velocity is not. I'm not exactly sure what this means, but x value of the relative velocity is caused by the rb.moveposition I have in my code.
    Capture.PNG
    What's the difference between relative velocity and the normal velocity?
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Velocity is the absolute velocity in the world. What you're looking at with relative velocity is one of the contacts on the rigidbody. That means it's data related to something your Rigidbody is touching.

    Since your velocity is very high (438) and your relatively velocity is pretty much zero (7.4e-6), It seems like the platform you're standing on is moving up very fast and your character is being pushed along with it.
     
  8. starwarsb2003

    starwarsb2003

    Joined:
    Sep 5, 2020
    Posts:
    8
    I can confidently say that the platform is not moving (they do not even have ridgidbodies attached to them). And the y-velocity starts of at 0 and stays there as the character falls onto the platform. My jump function is rb.AddForce(new Vector2(0f, jumpHeight), ForceMode2D.Impulse), so as I tap the screen several times the velocity builds up and doesn't go back down.
     
    Last edited: Oct 17, 2020
  9. starwarsb2003

    starwarsb2003

    Joined:
    Sep 5, 2020
    Posts:
    8
    I have found the problem and fixed it! The issue was with my using rb.movePosition to move the character horizontally. I'm not sure if it's intended but every time that ran the velocity was ignored, and it was within fixedUpdate so the velocity was always ignored.

    This bug? appeared when I switched from Unity 3D to 2D. Using both velocity and moveposition works perfectly in 3D but they seem to not work well in 2D. Thanks for trying to help PraetorBlue! :)
     
    PraetorBlue likes this.