- Home /
The question is answered, right answer was accepted
How can i to make Explosion just for 1 object??
I make simple Explosion :
Rigidbody myRb = hit.GetComponent<Rigidbody> ();
if (myRb != null)
{
myRb.AddExplosionForce (Force, explosionPos, Radius, Up);
}
How i can make Explosion just for 1 object ??, but not using layerMask. If i use layerMask, maybe iam gonna make 100 layer. Cause i want explode just for 1 object.
Answer by lunoland · Aug 11, 2016 at 05:30 PM
You need to add some state to your objects that distinguishes an object you want to explode from other objects with Rigidbody components.
class MyObject : Monobehaviour {
public bool canExplode = false;
}
Add the MyObject (or whatever you want to call it) script to all of your objects. In the inspector, set the canExplode flag to true for whichever objects you want to explode.
Then, change your code to check this value:
Rigidbody rigidbody = hit.GetComponent<Rigidbody>();
MyObject myObject = hit.GetComponent<MyObject>();
if (rigidbody != null && myObject != null && myObject.canExplode) {
rigidbody.AddExplosionForce(Force, explosionPos, Radius, Up);
}
Better still, you can move the explosion behavior to a method in MyObject, so if there are multiple events that can trigger an explosion, they are all maintainable from the same place.
Follow this Question
Related Questions
How to create an explosive force with Unity2D 4 Answers
Physics.OverlapSphere wrong radius 0 Answers
how create a explode force collider 2 Answers
How to calculate force from explosion on a rigidbody 1 Answer
Directional Explosion 1 Answer