Search Unity

AddExplosionForce Issue

Discussion in 'Scripting' started by DragonFlame, May 24, 2010.

  1. DragonFlame

    DragonFlame

    Joined:
    Mar 21, 2010
    Posts:
    18
    Hello, been having a probelm using AddExplosionForce for quite some time now.

    The problem im having is that I have a missle that when collides with a capsule collider it Adds an Explosion Force at the location of the missle on impact but for some reason it only affect the Y velocity. I am aware that there is an upwardsModifier but I have it set to 0 so I know thats not the problem. I have also checked that the explosion is in fact being created where the missle was.
    The player capsule doesnt have gravity applied to it and it is restricted to the x and y axis as its a 2.5d game.
    The odd thing is that on a very rare occasion it seem to work correctly for a while but then stops again after a few missle hits.

    Does anyone know what the likely cause would be?
    I have been searching the net and this forum for a few days now while trying many differnt things to get it to work. I know its most likely a simple thing to fix but I just cant work it out.

    Code (csharp):
    1.  
    2. private var radius : float = 1.5;
    3. private var power : float = 5.0;
    4.  
    5. var explosionPos = thisMissle.position;
    6. var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
    7.  
    8. //For Each Object it Hits and that has a Rigid Body apply Explosive Force.
    9. for (var hit in colliders) {
    10.     if (!hit)
    11.     continue;
    12.  
    13.     if (hit.rigidbody){
    14.         hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 0);
    15.     }
    16. }
    17.  
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    Can you post the section of code you are using to restrict the player to X/Y movement?
     
  3. DragonFlame

    DragonFlame

    Joined:
    Mar 21, 2010
    Posts:
    18
    Thanks.
    It seems I have just worked out the problem; trust me to do something like that after I ask for help.
    Such a simple thing I should have noticed it before.

    Thanks for the reply anyways.
    I was using a basic thisShip.position.z = 0; if you were curious. I tried thisShip.rigidbody.position.z = 0; also but it seems to do the same thing.

    For anyone that is having a similar problem, my ship has a capsule collider with a rigid body attached but also has a spherical trigger which is used to detect things around it which I totally overlooked, the way the AddExplosionForce is coded above it checks to see if it hit a rigid body regardless if its a trigger or not and applies the force.
     
  4. binary01

    binary01

    Joined:
    Apr 18, 2011
    Posts:
    2
    I am having the same issue as DragonFlame, but I do not understand his solution.

    I am building a FPS and the user can shoot 'fireballs' at an enemy who is essentially a floating egg. The enemy is a Prefab containing a Rigidbody and a Sphere Collider. The Rigidbody does not use gravity and the collider is not a trigger.

    The problem is that with a direct hit, the enemy only moves upwards in the y position and rotates. He does not move in the x or z.
     
  5. binary01

    binary01

    Joined:
    Apr 18, 2011
    Posts:
    2
    So it's not only a problem with my enemy prefab, I am just not using AddExplosionForce() properly? I am having the same issue with a cube object that does have gravity applied to it. When I apply AddExplosionForce(), it only 'explodes' upwards and not outwards at the same time.
     
  6. miroslaw

    miroslaw

    Joined:
    Nov 15, 2012
    Posts:
    70
    did u ever solve this? i'm having the same issue. here goes to hoping i'll get a reply
     
  7. misterkid

    misterkid

    Joined:
    Feb 15, 2012
    Posts:
    79
    I don't have any weird issue well didn't had any I got this.
    I hope it might have the solution in there :eek:
    It just works as expected.
    Code (csharp):
    1.         Collider[] colliders = Physics.OverlapSphere(this.gameObject.transform.position, explosionRad);
    2.         for (int i = 0; i < colliders.Length; i++)
    3.         {
    4.             if (colliders[i].gameObject != this.gameObject)//I should not hit myself!
    5.             {
    6.                 if (colliders[i].rigidbody)//It needs to be a rigedbody!
    7.                 {
    8.                     colliders[i].gameObject.rigidbody.AddExplosionForce(explosionForce, this.gameObject.transform.position, explosionRad, 3.0f);
    9.                 }
    10.             }
    11.         }
     
  8. Will_at_BridgeWorxGames

    Will_at_BridgeWorxGames

    Joined:
    Feb 20, 2020
    Posts:
    12