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): private var radius : float = 1.5; private var power : float = 5.0; var explosionPos = thisMissle.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius); //For Each Object it Hits and that has a Rigid Body apply Explosive Force. for (var hit in colliders) { if (!hit) continue; if (hit.rigidbody){ hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 0); } }
Hi, welcome to the forum! Can you post the section of code you are using to restrict the player to X/Y movement?
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.
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.
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.
I don't have any weird issue well didn't had any I got this. I hope it might have the solution in there It just works as expected. Code (csharp): Collider[] colliders = Physics.OverlapSphere(this.gameObject.transform.position, explosionRad); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject != this.gameObject)//I should not hit myself! { if (colliders[i].rigidbody)//It needs to be a rigedbody! { colliders[i].gameObject.rigidbody.AddExplosionForce(explosionForce, this.gameObject.transform.position, explosionRad, 3.0f); } } }
I had the same issue and was able to fix it using a forcemode! Check out my post here: http://answers.unity.com/answers/1785939/view.html