2

I'm creating a fast-paced, 2D side-scrolling game on Unity 5.2, building my terrain out of discrete "blocks", each with its own EdgeCollider2D component.

Having a problem where my character gets bumped upward as it crosses from one block to another (imagine driving your car over a speed bump on the road).

This doesn't happen all the time. Seems to be random, which is even more irritating, as it makes finding a solution more difficult.

I've tried all of the suggestions that I could find for similar questions on this site, including:

  • Using CircleCollider2D's on the character
  • making sure the terrain blocks and their corresponding colliders are perfectly aligned. The attached screenshot shows one of the intersections.
  • changing the "Min penetration for penalty" setting to the minimum allowed value (0.0001)
  • switching between discrete and continuous collision detection for the character's RigidBody2D
  • increasing the mass and gravity scale for the character's RigidBody2D

... to no avail.

Beyond building a single, massive terrain object with a single edge collider from start to finish (which I'm trying to avoid), I've run out of ideas. Anything else I'm missing? Is it just a Unity bug?

Help!

Terrain block intersection screenshot - zoomed in

Zoomed out - scene

nerv
  • 21
  • 3
  • Not uh...100% sure what your image represents. Looks like just a screenshot of an orthographic viewport...with an object in half the view? – Serlite Oct 22 '15 at 17:32
  • @Serlite It's a zoomed-in (and cropped) screenshot of where two terrain blocks and their colliders intersect - to show they are (all) perfectly aligned. I've edited the question and added a second, zoomed-out screenshot hoping that makes it clearer. – nerv Oct 22 '15 at 18:26
  • Are you using Vertex Snapping? http://docs.unity3d.com/Manual/PositioningGameObjects.html – user3071284 Oct 22 '15 at 18:30
  • @user3071284 yes. I have used Vertex snapping and also typed in all transport and collider points (via script) to ensure they line up. – nerv Oct 22 '15 at 18:38
  • Try changing to 3D/perspective camera and see if you can zoom in any further; sometimes I find the orthographic camera might not zoom in as far. Is the bumping happening between all blocks or just the ones that aren't flat? – user3071284 Oct 22 '15 at 19:03
  • Happens between all blocks, but at random. There have been a few runs in which it doesn't happen at all. I've typed in all coordinates for the blocks and colliders, to ensure that the points line up. Even switching to 3D, I see no gaps or bumps. I'm pretty floored. – nerv Oct 22 '15 at 19:13
  • This is a common issue in general with physics engines: when moving right, gravity is applied, making the player move into the "edge" of the next block. The collision resolution needs to decide how to push the object out, and sometimes gets it wrong because it doesn't know that the blocks are seamless. The universal solution is to make a big edge loop. I've seen an engine that will do this for you in the past, but I don't think there's an easy way with what Unity provides. – 31eee384 Oct 22 '15 at 19:27
  • Thanks for the additional info @31eee384. It makes sense. I don't know much about edge loops, so I'll have to read into that. In the meantime, if anyone else has more ideas, please share! – nerv Oct 23 '15 at 13:32
  • Not sure if this is helpful (depends on the type of game you are building) but you might even consider not using the physics engine at all. For example, a game like 'jetpack joyride' could easily be programmed without physics, and might even run smoother. – Kokodoko Oct 24 '15 at 13:03
  • Is each of these sections a separate GameObject with its own Rigidbody2D? If so, you might consider adding (reparenting) the next EdgeCollider2D to the currently traversed segment as you pass over it. You'll need to destroy (or at least remove) the Rrigidbody2D from the new section. And then keep on removing colliders again as they pass out behind. This way you are only ever dealing with one Rigidbody2D, it might help :) – davient Oct 29 '15 at 03:59
  • Curious if you ever got resolution to this problem? I've posted pretty much the exact same problem over on gamedev.stackexchange.com. – Electro-Bunny Oct 23 '16 at 22:09

1 Answers1

0

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);
}