Stopping an explosion from affecting a certain rigidbody.

So I have the following code:

function explosionThing() {	
	var colliders : Collider[] = Physics.OverlapSphere (transform.position, explosionRadius);
	for (var hit in colliders) { 	
		
		if (hit.rigidbody)  { 
			hit.rigidbody.AddExplosionForce(explosionPower, transform.position, explosionRadius);
		} 
	}
}

How would I go about making it so that the explosion force will not affect a certain rigidbody?

var bodyToIgnore : Rigidbody;

function explosionThing() {	
	var colliders : Collider[] = Physics.OverlapSphere (transform.position, explosionRadius);
	for (var hit in colliders) { 	
		
		if (hit.rigidbody != null  hit.rigidbody != bodyToignore)  { 
			hit.rigidbody.AddExplosionForce(explosionPower, transform.position, explosionRadius);
		} 
	}
}

Thanks a lot.

So, now that I have time to try it, I have. And it doesn’t work :(. Everything is just as effected as normal.

Try this:

var bodyToIgnore : Rigidbody; 

function explosionThing() {    
   var colliders : Collider[] = Physics.OverlapSphere (transform.position, explosionRadius); 
   for (var hit in colliders) {     
       
      if (!hit.rigidbody(bodyToIgnore))  { 
         hit.rigidbody.AddExplosionForce(explosionPower, transform.position, explosionRadius); 
      } 
   } 
}

Comes with this miskate:

Assets/Scripts/Shooting/Tank Shell.js(48,15): BCE0077: It is not possible to invoke an expression of type 'UnityEngine.Rigidbody'.

Huh…

That’s never happened to me. I’ll try the scripting docs. See if I can find an answer there.

Just make the object you want NOT to be affected by the explosion kinematic… isKinematic I think is the flag off of the top of my head.

1 Like

You must be getting an error because I noticed a mistake in my code. Here’s an updated version:

var bodyToIgnore : Rigidbody; 

function explosionThing() {    
   var colliders : Collider[] = Physics.OverlapSphere (transform.position, explosionRadius); 
   for (var hit in colliders) {     
       
      if (hit.rigidbody != null  hit.rigidbody != bodyToIgnore)  { 
         hit.rigidbody.AddExplosionForce(explosionPower, transform.position, explosionRadius); 
      } 
   } 
}

The error being the spelling mistake? Because I noticed that and fixed it before posting the unfortunate dact about it not working. When it is not 5:28am, and I am not on my iPod touch, I will we if it makes any difference i’d I use c#.

So I have converted it to C#, solved all problems except for one. How do I fix this error:

Assets/Scripts/Shooting/Tank Shell.cs(54,17): error CS0246: The type or namespace name `var' could not be found. Are you missing a using directive or an assembly reference?

Which is in the foreach loop.

void  explosionThing (){    
	Collider[] colliders = Physics.OverlapSphere (transform.position, explosionRadius); 
	foreach(var hit in colliders) {     
	if(hit.rigidbody != null) {
		if(!hit) continue;
		if (hit.rigidbody != bodyToIgnore)  { 
			hit.rigidbody.AddExplosionForce(explosionPower, transform.position, explosionRadius); 
		} 
	}
	} 
}

Actually, what do I define the hit as?

I think you were going in the right ditection with the first script.
maybe try assigning a tag to the object you don’t want it to effect then tell the physics push to ignore objects with that tag. i’m not sure what the code is for that is but it seems the most logical solution.
look it up on the scripting resources i’m sure you can tell the entire script to ignore it. rather than write code.
i could always be wrong

I defined hit as collider and it worked like the js version.

I put a debug.log in and found that it is hitting bullet(clone), and not bullet. Could this be the problem? and if so, how do I get around it?

I will try this right now.

Works like a charm, thanks a lot everyone.

Or i could be right lol awesome ive never actually done this myself to be honest it was a lucky guess based on notes i’ve read before.
glad I could help :smile:

Sorry, what exactly did he do ? My code always impacts all Rigidbody, and i need it to be not Kinematik.

Holy mother of necro posting, didn’t even notice till the last comment.

Tag the objects that you don’t want effected with something like Unmovable, before you add force say
if(obj.tag == Unmoveable)
return/continue/whatever

Sorry for no code tags, on mobile