If I throw a grenade on the ground, it’s forces would go in all directions from the point of the explosion.
Is there a way to accomplish this in Unity?
I looked into AddRelativeForce which adds a force in a single direction to a single object, and AddExplosiveForce which does the same for multiple objects in a radius, but how do I add a force that goes in ALL directions outwards from a single point?
Many thanks in advance
Your wording is a bit strange here. AddExplosionForce() goes in “all” directions one object at a time. That is, AddExplosionForce() only works on a single game object, but it adds force based on a world position and the distance a game object is from that position. The process is usually
- Pick a world point for the explosion
- Use OverlapSphere() to find all the colliers close to that point
- For each collider you use Rigidbody.AddExplosionForce(). The force applied takes into account the center of the explosion and the distance the collider is away from that point.
Take a look at the example code in the refrence for AddExplosionForce().
Ah, that is awesome. Thanks Robert
I had misunderstood. When I applied AddExplosionForce, the force went straight up, and I operated on the assumption that all objects in the sphere would go straight up. But I guess what I learned here is
assumption + code != answer
So the objects on the nearside of the explosion position (within the sphere) will go towards the camera and the objects on the far side of the explosion will go away from camera? If so that is just what I am looking for.
I’ll give it a go.
Thanks again. Most helpful.