- Home /
AddExplosionForce help!
Hi! I'm trying to get a button to initiate an explosion effect to nearby objects but thte method i'm using doesn't seem to work! This is the script i got so far:
using UnityEngine;
using System.Collections;
public class button1 : MonoBehaviour {
public float radius = 5.0f;
public float power = 10.0f;
void Start()
{
}
void OnGUI ()
{
if (GUI.Button (new Rect (10,600,100,20), "play"));{
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders)
{
if (!hit && hit.rigidbody)
hit.rigidbody.AddExplosionForce(1000, explosionPos, 10, 3.0F);
}
}
}
}
Answer by TykoX64 · Mar 19, 2014 at 02:46 PM
It seems your problem is here
foreach (Collider hit in colliders)
{
if (!hit && hit.rigidbody)
hit.rigidbody.AddExplosionForce(1000, explosionPos, 10, 3.0F);
}
This is like asking if (collider == null && has a rigidbody). You can remove the check for the collider since it's a list of colliders, just check if it has a rigidbody. If it does, then it's effected...(affected?)
foreach (Collider hit in colliders)
{
if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(1000, explosionPos, 10, 3.0F);
}
Thanks for the answer but it still doesn't work! there are no errors with the script it just doesn't do anything when I press my button.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Applying explosion force to destructible object AFTER it's been destroyed 2 Answers
Stop certain Axis Force in a certain condition. 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Point Effector 2D is inconsistent 0 Answers