I'm doing procedural terrain generation. So far I've created the mesh, and attached a 2D polygon collider to it. The terrain is made up of hills going up and down.

alt text

After doing so, I put a 2D rigid body, along with a 2D box collider to represent the player. When I put the player on top and let him slide all the way down, sometimes he doesn't slide properly, and instead bumps on the hill, jumping all of a sudden on his own as if he tripped on something (even though there's nothing there). What's weird is that it doesn't happen all the time, just sometimes. Even when I make him slide on the same spot he bumped before again, he wouldn't do it again except on rare occasions.

What could be causing this?

  • \$\begingroup\$ Is your FPS spiking? Have you checked your stats window. I doubt that you are doing anything behind the scenes that could cause it to rocket but the culprit seems to be the box collider, as it is attached to your player and controls most of his translations. Not everything is a cure-all in Unity so you might have to roll your own. \$\endgroup\$ – Grey Feb 4 '14 at 20:34
  • \$\begingroup\$ Also consider drawing your box to see if it intersects with your terrain at any point in time, as it may be one of the unexpected behaviors causing your character to trip. \$\endgroup\$ – Grey Feb 4 '14 at 20:35
  • \$\begingroup\$ No FPS spikes. As far as the box collider goes, shouldn't the physics stop it from intersecting with the other collider? \$\endgroup\$ – Pat Feb 5 '14 at 7:04
  • 1
    \$\begingroup\$ I had a similar issue when I had a box collider traveling along a sloped 2d terrain. I changed my box collider to a circle collider and that seemed to stop it but my sprite didn't need rotate to match the slope... \$\endgroup\$ – Savlon Feb 18 '14 at 8:51
  • 2
    \$\begingroup\$ Savlon, I managed to solve the issue by doing the same thing you did. I had to manually rotate the object to math the slope though. I'll write up how I did it in an answer when I have some time. \$\endgroup\$ – Pat Feb 18 '14 at 9:04

I had similar issue, but my character stopped at some points. After adjusting rigidbody mass and physics material friction, it worked. Maybe it will work in your case.


Try detect the collision and set the vertical velocity to zero.

void OnCollisionEnter2D(Collision2D col)
        if (col.gameObject.name.StartsWith("block"))
            rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.