Why isn't AddExplosionForce working?
I am trying to get it so that if the block gets hit by the raycast (Executed earlier in the script), It explodes, exploding other blocks too. Sure enough, it goes sailing, but nothing else does. What's wrong?
if (hit.collider.tag =="Boom"){
var explosionpos = new Vector3(0,0,0);
explosionpos = hit.rigidbody.transform.position;
explosionpos[2] = explosionpos[2] + 5;
hit.collider.gameObject.GetComponent.<Rigidbody>().AddExplosionForce(1000,explosionpos,20050,3.0F);
Destroy(hit.collider.gameObject, 2);
}
Answer by $$anonymous$$ · Jun 23, 2016 at 08:09 PM
You have to apply the explosionforce to all the objects within range of your explosion, not only the box that gets hit by the raycast. Use
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
to find all the colliders within reach and add the explosion force to each one of them.
There is a good example in the documentation: https://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html
Your answer
Follow this Question
Related Questions
Making UI block rays with touch inputs 0 Answers
Figuring out what the acceleration will be before applying force to RigidBody2D 0 Answers
hit.point returns wrong coordinates 0 Answers
Raycasting Animations 0 Answers
RayCast and Animation not working 0 Answers