- Home /
AddExplosionForce only goes up
I've got a projectile that explodes when it hits any collider. The problem is that it only seems to produce force in the +y direction. I've tried leaving out the upwardsModifier and setting it to zero. Neither helped. My code:
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);
foreach(Collider hit in colliders)
{
if (!hit)
continue;
if (hit.rigidbody)
{
if(hit.rigidbody != this.rigidbody)
{
hit.rigidbody.AddExplosionForce(power, explosionPos, radius);
Debug.Log(hit);
}
}
}
I can see that it's hitting the correct collider from the debug statement.
As a start between lines 11 and 12, insert:
Debug.DrawRay(transform.position, hit.transform.position - transform.position, Color.red, 5.0);
The red line will display for 5.0 seconds and should be aligned with the explosion force. This will allow you to visualize your geometry to make sure it is as you expect.
The red line is going from the point of impact to the center of the collider for the hit object. That is where I would like the explosion to force to head, but I don't think it's actually doing that. AddExplosionForce is mysterious...
As a text, replace your AddExplosionForce() with:
hit.rigidbody.AddForce((hit.transform.position - transform.position).normalized * 100.0f);
If this also does not work, check to see if your object is colliding with anything or if a script on the individual objects is impacting movement. For example freezing x and z movement of the rigidbody would cause this behavior.
Yes, I am freezing movement in the z axis. I will try turning that off to see how it reacts. If freezing the z axis is causing the problem I'll have to apply forces manually.
Your answer
Follow this Question
Related Questions
What are the units of power in Physics.AddExplosionForce? 1 Answer
AddExplosionForce constantly like a fan 1 Answer
How can use AddExplosionForce to explode scattered objects? 1 Answer
Default Unity Rigidbody.AddExplosionForce script problem 1 Answer
How to I calculate damage when using AddExplosionForce? 0 Answers