1
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\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
    Commented Feb 4, 2014 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
    Commented Feb 4, 2014 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
    Commented Feb 5, 2014 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
    Commented Feb 18, 2014 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
    Commented Feb 18, 2014 at 9:04

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ In what way did you adjust those parameters? According to the comments, it seems like the asker already has a different solution too... Some more detail would help. \$\endgroup\$
    – Anko
    Commented Oct 17, 2014 at 13:22
0
\$\begingroup\$

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);
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .