I’ve a specific state of affairs the place I need to use a set off to regulate the course of a fast-moving projectile, however am working into points as a result of Unity doesn’t assist steady collision detection for triggers.
Think about we have now a skinny wood door. We wish a bullet to have the ability to go by way of the door, however the bullet’s velocity ought to be diminished and trajectory altered when this happens. We’d put a set off collider on the door, with some code like this:
void OnTriggerEnter(Collider different) {
if (different.tag == "Bullet") {
Rigidbody physique = different.GetComponent<Rigidbody>();
physique.velocity = physique.velocity * .75f;
float maxRot = 45;
float rx = Random.Vary(-maxRot, maxRot);
float ry = Random.Vary(-maxRot, maxRot);
Quaternion rotationChange = Quaternion.Euler(new Vector3(rx, ry, 0));
physique.velocity = rotationChange * physique.velocity;
rework.LookAt(rework.place + physique.velocity.normalized);
}
}
The issue is that Unity doesn’t assist steady collision detection for triggers. If the bullet is transferring quick, the set off impact will not be utilized till the bullet is nicely previous the door (it appears to get utilized a body late), or the bullet could go from one facet of the set off to the opposite with out ever touching the set off.
Altering the Rigidbody collision detection mode (e.g. "steady", "steady dynamic") has no impact on triggers. I’ve already tried each mixture of collision detection modes on each the bullet and the door.
I am hoping for an excellent workaround that doesn’t carry a excessive efficiency price. Options I’ve thought-about:
- Improve the FixedUpdate frequency dramatically (costly)
- Use raycasts to simulate steady collision detection (may get costly with a lot of bullets)
- Use a thicker set off collider on the door (helps, however doesn’t get rid of the difficulty, and provides an inconsistent expertise with completely different speeds of bullets)


