2

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:

The player is moving left to right when the collision occurs

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:

  1. Using a single object instead of many
  2. 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?

1 Answers1

-1

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

LeetCoder
  • 39
  • 2
  • Yeah, this is one of those simple solutions I mentioned that won't work because of the nature of the game. The Segment objects resize dynamically, so joining it to another Segment would interfere with this feature.... –  Feb 11 '21 at 12:54