- Home /
AddExplosionForce makes affected object collide with everything at once?
Hello friends!
In my project I can throw a projectile (with Force) that I can also pick up again. The collision detection on the projectile is set to continuous dynamic. On collision with an enemy, the projectile becomes kinematic and after a timer the enemy explodes and emits an explosion force to nearby objects:
private void OnDestroy()
{
if (projectile != null)
{
projectile.GetComponent<Rigidbody>().isKinematic = false;
}
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
foreach (Collider inRange in colliders)
{
Rigidbody rb = inRange.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(explosionForce, transform.position, explosionRadius);
}
}
}
IsKinematic is set to false so the projectile is also affected by the explosion. One or two (or sometimes a few more) frames after the force explosion is called, the projectile returns a collision with (almost?) every single game object in the scene including every other enemy; triggering a chain reaction of explosions. This, of course, is not a welcome feature... Other gameObjects affected by the explosion do not share this problem.
The projectile's collider has the size of a parentless cube with scale (0.02564212, 0.08199235 0.6880991). When I scale the projectile to be larger it seems to fix the issue, suggesting it might be too small for the physics engine to handle properly... But the force added to the projectile when throwing it does not create this problem and making it larger would be really inconvenient.
Is there a way I could make this work?
It's been a while since I used Unity so I don't know if this is relevant information any longer but in the past at any rate, and if I remember correctly, there used to be issues with using non-uniform scale (that is when xyz scale is not the same across each of the values). As a test, if possible, try making your colliders scale uniform.
Hey @Statement, thanks for your reply! :) I did not realize non-uniform scaling was so undesirable. I changed the Scale Factor of the model Import Settings (to 2.5 in case that's relevant) so that the game object has a uniform scale of 1 in the scene. I tried giving the Box Collider different uniform scales (also tried a compound collider consisting of multiple uniform box colliders on child objects along the mesh to fit the shape, which felt weird when opposed to just one non-uniform box collider) but sadly, the problem persists at the lower scale I'm ai$$anonymous$$g for.
What remains true is that the bigger I make the collider, the less the problem occurs... Suggesting the projectile is just too small for (explosion?) force to apply correctly. even though it does not seem that small from my subjective point of view... :p
Still don't really know how I would tackle the issue.
Your answer
Follow this Question
Related Questions
Bullet projectiles with collision info without affecting others 0 Answers
Projectiles, their speed, and collisions 1 Answer
Collision occurring when it should not! Help! 1 Answer
Giving Instantiated Object Velocity Causes Wrong Position 1 Answer
Physics - increase gravity speed without making balls fall through planes 1 Answer