- Home /
Other
Unity3D grenade not exploding correctly
So I have my grenade script, and its working but not great. I got my ragdoll that Im testing the grenades on and that ragdoll is moving in the direction that Im throwing from, even if I throw a grenade infront of him and Im standing behind him, he is moving forward...How can I make this so he moves in the direction depending on where the bomb is?
public class GrenadeThrow : MonoBehaviour
{
public Rigidbody Grenade;
public Transform GrenadeBarrel;
public float timeBetweenShots = 0.03f;
private float timestamp;
public int GrenadeAmmo = 5;
public float radius = 5.0F;
public float power = 10.0F;
void Update ()
{
if (GrenadeAmmo > 0)
{
if (Input.GetKeyDown (KeyCode.G))
{
Rigidbody GrenadeInstance;
GrenadeInstance = Instantiate(Grenade, GrenadeBarrel.position, GrenadeBarrel.rotation) as Rigidbody;
GrenadeInstance.AddForce(GrenadeBarrel.forward * 700);
Invoke("Explosion", 2);
timestamp = Time.time + timeBetweenShots;
GrenadeAmmo--;
}
}
}
public void Explosion()
{
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders)
{
if (hit && hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);
}
}
}
I have checked the Unity Script Reference, and thats where I got the "Exploding" part from. And there is one more thing, the explosion has no effect on the ragdoll if Im standing far away and the bomb lands next to him, but if I walk up with my FPS controller next to him and throw a bomb AT THE SAME PLACE, it blows the ragdoll away...
Not only is the explosion originating from the thrower, so is the force of it. You're throwing a grenade object, then creating an explosion at your position.
Try to imagine yourself as being the script. What happens and what do you do, from your perspective. That tends to work sometimes. It's quite obvious you need to either split this up into multiple scripts or use some form of collection.
Answer by Sveyh · Apr 16, 2014 at 12:46 PM
Im so sorry for being a retard, I just added line 28 down to 35 into my "destroy" script which causes the explosion, and it works now, Im sorry lol, thanks anyways
Follow this Question
Related Questions
2D explosion that targets specific gameobjects 1 Answer
How do you make a ragdoll blow apart? 1 Answer
Null reference and GetComponent 1 Answer
OverlapSphere not causing damage? - Solved 2 Answers
Raycast collider detector lags horribly? 2 Answers