0
\$\begingroup\$

I am experiencing inconsistent behavior using Unity's 2D Physics engine and their Collider2Ds to detect collisions and triggers on my projectiles. I will classify it into two problems. The first is that occasionally the projectile will pass through walls/objects it should collide with. The second is that occasionally the projectile will pass through enemies that it should hit without triggering. I will try to elaborate in detail below.

A projectile is organized into a container Projectile GO and two children GO: a PhysicsColliders GO and a HitBoxColliders GO.

The PhysicsCollidersGO job is to hold the colliders that define the solid regions of the projectile using Collider2D components. It's GO is on a Projectile layer which is set to collide with other solid layers such as the ones my walls are on. Walls are static colliders.

The HitBoxCollidersGO job is to define the a HitBox area. It's GO is on a HitBox layer and also uses Collider2D components that are set to be triggers. A HitScript on the GO listens for OnEnterTrigger.

Projectiles are set in motion by setting the velocity of the RigidBody once upon firing.

Here is a visual of the structure:

 ProjectileGO
  - PhysicsCollidersGO
  - HitBoxCollidersGO

Here are descriptions of the GameObjects and their components:

 ProjectileGO
   - RigidBody2D Component (collisionDetectionMode is set to Continious)

 PhysicsCollidersGO (Layer is set to Projectile)
   - BoxCollider2D Component

 HitBoxCollidersGO (Layer is set to HitBox)
   - BoxCollider2D Component (IsTrigger is set)
   - HitScript Component

Problem 1. Projectiles will sometimes not collide with walls/solid objects/etc...

Problem 2. Projectiles will sometimes not trigger a hit.

What could be causing these inconsistencies? How can ensure that collisions and triggers will consistently behave correctly?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I'd like to start with making the assumption that these objects are traveling relatively quickly, I have seen similar behavior and was able to get more reliable physics interactions doing a combination of the following three solutions.

First off - since physics interactions are calculated at fixed intervals, reducing the speed of fast moving objects (not always possible) can greatly reduce these issues.

If thats not an option, (or in conjunction with reducing the speed) you should look to changing the Collision Detection Mode on the Rigidbody/2d Component

RigidBody Inspector

  • We have a few options but by default unity chooses to use a less resource intensive method called Discrete. You can chose which Rigidbody objects you would like to increase the collision "sensitivity on" and ramp that mode up to Continuous or Continuous Dynamic and read more about them here in the documentation I have set objects with high priority interactions, Players, Enemies,Projectiles to continuous with some success.

If for some reason continuous is not enough you can opt in to a more performance requiring option by scaling down the Unity "Fixed Time Step" for your project... ultimately asking Unity to do more physics computations per second.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .