Search code examples
c#unity-game-enginecollider

Collision Detection for fast moving game object in Unity


I am working on building a basic space shooter game but am having trouble with a trigger collider.

In the game, I have a Bullet prefab that has a Capsule Trigger Collider attached to it and the enemy is right now a basic cube with a box collider attached to it.

When I run the game I start shooting the bullets and the Enemy cube doesn't disappear until after a few shots.

Here is my code for the bullet prefab:

void OnTriggerEnter(Collider col) {
        if (col.tag == "Enemy") {
            Destroy (col.gameObject);
        }
    }

And a screenshot of my Attributes of each game object:

Bullet Prefab:

Bullet Prefab

And here is the enemy cube:

enter image description here

Here is a link to a video of what is happening...

https://youtu.be/NjHK6oVP0OQ


Solution

  • I understand that this question may be similar to the one posted in the comments however I did find another solution that may be of some use to people who have this exact issue.

    The error was occurring because my "Bullet" prefab was moving so fast that it wasn't able to detect a collision.

    Fix: I changed the Collision Detection property of the Bullet prefabs Rigidbody to Continuous Dynamic because it is moving fast. Full reference to Rigidbody Collision Detection Modes the link is below.

    enter image description here

    https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html

    The difference between this and the "similar" question's answer posted in the comments is that answer has a lot to do with Raycasting versus very simple colliders (which is more of what I was looking for).