Unity 5.2 - Moving character bumps between 2D edge colliders - unity3d

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!

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

Related

Fixing ghost collisions on a plane made of multiple Colliders?

Basic Problem: Unity's physics engine produces weird collisions when a player is moving over a flat surface made of more than one Collider. The ghost collisions occur at the joints between Colliders, and express as two behaviors:
This seems to be a problem with physics engines in general, based on this talk by Bennett Foddy:
https://www.youtube.com/watch?v=NwPIoVW65pE&ab_channel=GDC
Game Specifics:
In my case, the player is moving through a procedurally generated wormhole, composed of Segment objects using a MeshCollider. The wormhole twists randomly through 3D space, while the width and height change dynamically. The player can strafe 360 degrees around the inside of the tunnel (direction of gravity is relative to position).
This makes the simpler solutions I've found impractical. Those include:
Using a single object instead of many
Placing redundant Colliders behind the joints
I've managed to flag these erroneous collisions in OnCollisionEnter(). This method on the PlayerController works fine to identify these erroneous collisions, and raise a flag.
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag != "Tunnel"){return;}
// Bit mask for tunnel layer.
int tunnelLayerMask = 1 << 10;
// Get the direction from the nearest Segment's origin to the collision point.
Vector3 toCollision = other.contacts[0].point - nearestSegment.transform.position;
if (Physics.Raycast(nearestSegment.transform.position, toCollision, out RaycastHit hit, 100f, tunnelLayerMask))
{
// Flag the collision if the detected surface normal
// isn't equal to the collision normal.
if (other.contacts[0].normal != hit.normal) { colFidelityFlag = true; }
}
}
But I'm at a complete loss when it comes to gracefully resolving the issue.
Currently, I'm just caching the player's velocity each frame. If a collision is flagged, I overwrite the resulting velocity with the cached velocity from the previous frame. This works for most conditions: the player ghosts imperceptibly into the floor for a frame, and gets past the offending joint.
But under high enough velocities or interactions with obstacles inside the tunnel, it is possible for the player to be ejected through the floor, and into space. I don't want to limit the velocity of the player too much, since the feel of the game partially relies on high velocity and hard collisions.
Does anyone know of a better way to resolve these erroneous collisions?
This is called "ghost vertices", and happens if you have like 2 box colliders (or something equivalent) and they are connected to each other. You could try to join the colliders so that instead of 2 separate connected colliders you have a single one.
Here ghost vertices are explained in more detail: https://www.iforce2d.net/b2dtut/ghost-vertices

Unity: 2D character floating using BoxCollider2D

I'm pretty new to unity and I set up a simple scene with my character and the ground. I added a Rigidbody2D and a BoxCollider2D to the character and another BoxCollider2D to the ground. The boundaries of the Colliders fit the boundaries of the character / ground exactly, but my character is not falling on to the ground, when I hit play, but rather stops and floats a few pixels above it.
I've seen a lot of similar questions but the solutions didn't work for me (or maybe I didn't understand them right, as I mentioned above I'm new to Unity and thats my first game).
That's what the floating looks like:
I could obviously "fix" this by lowering the upper boundary below the actual boundary of the ground, like this:
But that's obviously not a good solution and I'd still like to know what is causing this bug and how to fix it.
Maybe gap has size of Default Contact Offset:
Set a proximity distance value for colliders to be considered in contact, even they are not actually in contact. Colliders whose distance is less than the sum of their contactOffset values generate contacts. This allows the collision detection system to predictively enforce the contact constraint even when the objects are slightly separated.
Caution: Reducing this value too far could cripple Unity’s ability to calculate continuous polygon collisions. Conversely, increasing the value too much could create artifacts for vertex collision.
You can find it in Edit > Project Settings > Physics2d .

Unity player boxcollider 2d Contacts Normal Vector bug

This bug occurs very rarely (probably 1 in 100 collisions with a platform) where the players box collider 2d have contacts with the platform horizontally (two contacts with (-1,0) vectors and two (intented) with(0,1)). This is very problematic because the player cannot jump forward (just jumps up without any x velocity) and it looks like it hits an invisible wall. I tried increasing the default contact offset in the physics 2D settings to 0.01 but it did not help. I am creating a pixel art game with 16 Pixels per unit.
After researching I finally found a fix for my problem. My problem was that the player got stuck inbetween my tiles each having a boxcollider with the TilemapCollider2D. I fixed it by removing each collider to one whole with a composite:
add TilemapCollider2D to the tile layer's component
check "used by composite" field in TilemapCollider2D
set Rigidbody2D to static
I had to check composite collider to polygons because otherwise I could not apply any Force to the rigidbody somehow.

Unreal Engine 4 - Collider component sweep issue

I have a 2D environment where an object falls (a trap) and a box collider component is moved with a timeline to follow the movement with SetRelativeLocation.
Since the trap is near a wall if I leave the Sweep option unchecked in some cases the character will stuck mid air between the trap and the wall.
Sweep off
If I turn on Sweep this will not happen and the character is pushed to ground correctly but since the collider "slows down" for some frames it seems to be in the middle of the sprite.
Sweep on
Teleport option seems to have no effect on this behaviour.
Is there any way to have the sweep effect with the collider keeping his velocity and keeping the structure as it is with the collider moved by a timeline?
I feel like I'm missing something really simple but I'm losing my head on this.
Thanks!
Since it's 2D, have you tried blocking the input when your character collides? I would add a simple collider in front of your character and when it is overlapped, I'd limit the input in the direction your character goes, ie.:
Character collides right
MoveRight(float Scale): Clamp Scale to (-1.f, 0.f); AddMovementInput(...
Hope it helps!

Unity 4 Character Mesh Collider not aligned - character falls through. Image attached inc source unity asset link

Hello Peeps (first post yay).
I have just started to learn about unity and still going through various tutorials and learning the interface along with C#.
I am running Unity 4 and downloaded a free asset from the Unity store:
https://www.assetstore.unity3d.com/#/content/3174
The Problem.
I created a Plane in Unity.
Went to the Character and created colliders and imported it as a humanoid.
Then added a rigid body = The character fell through the plane
I then added a Mesh collider and attached the mesh for the character to the collider. The mesh does NOT align correctly to the character = the character fell through the plane.
I then tried box and capsule colliders and they worked fine. Attached these and the character would not fall through.
I then tried a character controller = Character did not fall through.
I want to have an accurate mesh for this test character so when an enemy hits him, there is an actual hit.
I have tried several other free characters from the store and they all have the same problem. Can someone please tell me how I can fix this? It seems I just either doing something completely wrong, missing a step or just plain lost.
Link below to image of the issue taken:
Using a mesh collider on a skinned, animated character is a wrong approach. Mesh colliders are generally used for environment geometry and rigid objects. This is all due to the amount of work it is needed for the Unity engine to accurately calculate the shape of the mesh collider, based on the provided mesh. More triangles means more work, and for the reasonably complex low poly character mesh of 10k to 30k triangles it is clear that the fps would most definitely drop to an unplayable level.
For our game we are using all primitive colliders, distributed on the adequate bone game objects. So, head bone has a sphere collider, spine has a box collider (for lungs), forearm has a capsule collider etc. That way,with a little bit of scripting, you can detect a hit and know exactly what part of body is damaged.
If your collider doesn't exactly match your mesh, then it's likely that you have the mesh applied to the wrong part of the object.
Try applying the Mesh Collider to the mesh, instead of the complete object.
You have a pretty complex character mesh, so using mesh collider probably isn't the best way to go. In many cases, character controller or capsule collider works fine; otherwise, you can just use compound collider and create a box or a sphere around various body parts of the character.
This once happened to me. The mistake i made was, i applied the mesh collider after i did some rotation and scaling. So i added the mesh collider as soon as i imported the model. And then it should be alright when you scale/edit the model. Also check with right/left handed co-ordinate system with unity and 3D MAX/Maya.