Apek
May 13, 2014, 6:24am
1
My script goes as follows
private void Explode()
{
Collider thingsWeHit =
Physics.OverlapSphere(transform.position,
explosionRadius,
explosionDetectionMask);
for(int i = 0; i < thingsWeHit.Length; i++)
{
Rigidbody hitRigidbody = thingsWeHit*.rigidbody;*
if(hitRigidbody != null)*
{*
hitRigidbody.AddExplosionForce(explosionForce,*
transform.position,*
explosionRadius,1f,ForceMode.Impulse);*
if(hitRigidbody.gameObject.layer == LayerMask.NameToLayer("Enemy"))*
{*
Destroy(hitRigidbody.gameObject, .75f);*
}*
else if(hitRigidbody.gameObject == null)*
{*
Destroy(this.gameObject, 2f);*
}*
}*
}*
I want it to destroy itself after 2 seconds if it doesnt collide with any rigidbody so it doesnt fly on forever. Any help would be appreciated
OSG
May 13, 2014, 7:39am
2
Try this
private void Explode() {
Collider[] thingsWeHit = Physics.OverlapSphere(transform.position, explosionRadius, explosionDetectionMask);
for(int i = 0; i < thingsWeHit.Length; i++)
{
Rigidbody hitRigidbody = thingsWeHit*.rigidbody;*
if(hitRigidbody != null)
{
hitRigidbody.AddExplosionForce(explosionForce,
transform.position,
explosionRadius,1f,ForceMode.Impulse);
if(hitRigidbody.gameObject.layer == LayerMask.NameToLayer(“Enemy”))
{
Destroy(hitRigidbody.gameObject, .75f);
}
}
}
if(thingsWeHit.length == 0)
{
Destroy(this.gameObject, 2f);
}
}
–EDIT-- This code will destroy your object when u call explode function and an abject doesn’t hit any enemy at this moment
why not just put
void Start()
{
Invoke("SomeMethod", 2.0f);
}
void SomeMethod()
{
Destroy(gameObject); //if you want the script to be destroyed
// put 'Destroy(this);' instead
}
anywhere in your script pretty much. This should get the job done.