Ok I finally put it in code form.
This script goes on the grenade
var radius : float = 5.0; //provides a radius at which the explosive will effect rigidbodies
var power : float = 10.0; //provides explosive power
var explosiveLift : float = 1.0; //determines how the explosion reacts. A higher value means rigidbodies will fly upward
var explosiveDelay : float = 5.0; //adds a delay in seconds to our explosive object
var explosionPrefab : Transform;
var explosionSound : AudioClip;
function Update () {
Fire();
}
function Fire () {
yield WaitForSeconds(explosiveDelay);
Instantiate(explosionPrefab, transform.position, transform.rotation);
var grenadeOrigin : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (grenadeOrigin, radius); //this is saying that if any collider within the radius of our object will feel the explosion
for(var hit : Collider in colliders){ //for loop that says if we hit any colliders, then do the following below
if (hit.GetComponent.<Rigidbody>()){
hit.GetComponent.<Rigidbody>().AddExplosionForce(power, grenadeOrigin, radius, explosiveLift); //if we hit any rigidbodies then add force based off our power, the position of the explosion object
AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1);
Destroy(gameObject); //the radius and finally the explosive lift. Afterwards destroy the game object.
}
}
}
This script goes on the character or an empty game object which is the throw script
var grenade : Rigidbody; //our prefab
var throwPower : float = 10; //power variable determines how fast this object will be "shot out"
function Start () {
}
function Update () {
if (Input.GetKeyDown(KeyCode.G)) {
var clone : Rigidbody; //variable within a variable
clone = Instantiate(grenade, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.forward * throwPower); //applies force to our prefab using the throwPower variable
}
}
And this script goes in the explosion prefab of the grenade script. I keep getting a spam error saying CanDestroy cannot be defined
using UnityEngine;
using System.Collections;
public class PhysicalExplosion : MonoBehaviour
{
public float Radius;// explosion radius
public float Force;// explosion forse
void Update ()
{
Collider[] hitColliders = Physics.OverlapSphere(transform.position, Radius);// create explosion
for(int i=0; i<hitColliders.Length; i++)
{
if(hitColliders*.CompareTag("CanDestroy"))// if tag CanBeRigidbody*
{
if(!hitColliders*.GetComponent())*
{
hitColliders*.gameObject.AddComponent();*
}
hitColliders*.GetComponent().AddExplosionForce(Force, transform.position, Radius, 0.0F); // push game object*
}
}
Destroy(gameObject,0.2f);// destroy explosion
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position,Radius);
}
}