Search code examples
unity-game-engineunityscript

Unity Javascript Making a Good Collider


So, I am working on a game in Unity and I have some issues with my colliding. The problem is that the car that I made will sometimes glitch next to the wall and wont move back or forward. even though it's not colliding. What I've done is I added 2 empty gameobjects. Named them front and back bumper and added a script that is activated when the bumpers hit a wall. If the front bumper hits a wall it can't go forward and if the back one hits a wall it can't go backwards. But this isn't really that good. I need to know how to setup a good collider because this really is annoying when you play. Also the car can go up to 50 speed.


Solution

  • Assuming that the car can collide at high speeds into the wall, I would consider using a specific collision detection for the car's rigidbody.

    There are some collision detection methods that are used to prevent fast moving objects from passing through other objects without detecting collisions.

    From Unity's documentation :

    • Discrete : Use Discrete collision detection against all other colliders in the scene. Other colliders will use Discrete collision detection when testing for collision against it. Used for normal collisions (This is the default value).
    • Continuous : Use Discrete collision detection against dynamic colliders (with a rigidbody) and continuous collision detection against static MeshColliders (without a rigidbody). Rigidbodies set to Continuous Dynamic will use continuous collision detection when testing for collision against this rigidbody. Other rigidbodies will use Discreet Collision detection. Used for objects which the Continuous Dynamic detection needs to collide with. (This has a big impact on physics performance, leave it set to Discrete, if you don’t have issues with collisions of fast objects)
    • Continuous Dynamic : Use continuous collision detection against objects set to Continuous and Continuous Dynamic Collision. It will also use continuous collision detection against static MeshColliders (without a rigidbody). For all other colliders it uses discreet collision detection. Used for fast moving objects.

    As you can see, you should use Continuous detection for the walls and Continuous Dynamic detection for the car.

    Warning : Don't forget that Continuous detection has a big impact on performance, you should only use it if you have collision issues and in the minimum possible amount of objects.