- Home /
timed bomb, explosion detection
Im still quite new to coding in general, so I apologize for my improper indentation and etc. I learn somewhat from trial and error... but with something a little more complex like this im lost.. This script is planned to be used for multiple types of projectiles
so far i have the "on hit specific object hit" working...
but im stuck on if i set it up for timed explosion, how to get it detect nearby colliders and if "x" collider gets hit apply damage type of deal.
Heres what i got so far, my issues start at line 34 - 49
var bulletLife : int;
var bounces : int;
var explodeOnHit : boolean = true;
var explosionPrefab : Transform;
private var creationTime = Time.time;
var sound : AudioClip;
var damage = 10;
var areaOfEffectDamage : boolean = false;
var explosionRadius : int;
private var startTime;
function Awake () {
}
function Update () {
if (bounces==0){
var explosion = Instantiate(explosionPrefab, transform.position, transform.rotation);
Destroy(this.gameObject, 0);
AudioSource.PlayClipAtPoint(sound, transform.position);
Debug.Log("boom");
//AreaDamageEnemies();
}
if (Time.time > (bulletLife+creationTime)) {
Destroy(this.gameObject, 0);
AudioSource.PlayClipAtPoint(sound, transform.position);
var explosion2 = Instantiate(explosionPrefab, transform.position, Quaternion.Euler(Vector3(0, 0, 0)));
Explode();
}
function Explode () {
GameObject.Find("Tank 01");
var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
for (var hit : Collider in colliders) {
if (Blocked(hit) != 0)
continue;
//where im gonna apply damage
if (hit.rigidbody)
Debug.Log("area effect boom");
}
}
}
//Basic collision detection checking for two differently named objects
function OnCollisionEnter(theCollision : Collision){
if (explodeOnHit==true && theCollision.gameObject.name == "Tank 01"){
var explosion = Instantiate(explosionPrefab, transform.position, transform.rotation);
Destroy(this.gameObject, 0);
AudioSource.PlayClipAtPoint(sound, transform.position);
Debug.Log("tank hit");
}
if(theCollision.gameObject.name == "Cube"){
Debug.Log("Hit the cube");
}else if(theCollision.gameObject.name == "Plane"){
Debug.Log("Hit the wall");
}
else if (theCollision.gameObject.name == "Tank 01") {
theCollision.gameObject.SendMessage("OnDamage", damage);
}
bounces-=1;
}
function PlayAudio () {
}
Answer by RetepTrun · Dec 16, 2013 at 01:37 PM
My grenades use
function Start(){
invoke("Explode",4)
}
function Explode(){
explosion stuff..
}
Answer by GameVortex · Dec 16, 2013 at 02:53 PM
With the OverlapSphere function you have already found all colliders within range of the explosion, and then you can just use the same message as you are using on line 74 to damage to the object, and also check the name as normally:
function Explode () {
GameObject.Find("Tank 01");
var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
for (var hit : Collider in colliders) {
if (Blocked(hit) != 0)
continue;
//where im gonna apply damage
if(hit.name == "Tank 01") {
hit.SendMessage("OnDamage", damage);
}
if (hit.rigidbody)
Debug.Log("area effect boom");
}
}
EDIT Turns out my gameobject needed a collider on the parent object
Okay, so that almost works.. i made a few syntax errors but i got that sorted.
the message "area effect objects" plays when in range of a cube with a rigidbody attached..
however its not detecting the "Tank 01" game object with rigidbody attached...
heres the updated code just the update() part
function Update () {
if (bounces==0){
var explosion = Instantiate(explosionPrefab, transform.position, transform.rotation);
Destroy(this.gameObject, 0);
AudioSource.PlayClipAtPoint(sound, transform.position);
Debug.Log("boom");
//AreaDamageEnemies();
}
if (Time.time > (bulletLife+creationTime)) {
Destroy(this.gameObject, 0);
AudioSource.PlayClipAtPoint(sound, transform.position);
var explosion2 = Instantiate(explosionPrefab, transform.position, Quaternion.Euler(Vector3(0, 0, 0)));
AreaDamage();
}
}
function AreaDamage () {
GameObject.Find("Tank 01");
var colliders : Collider[] = Physics.OverlapSphere (transform.position, explosionRadius);
for (var hit : Collider in colliders) {
//if (Blocked(hit) != 0)
// continue;
if (hit.name =="Tank 01"){
hit.Send$$anonymous$$essage("OnDamage", damage);
Debug.Log("area effect tank01 hit");
}
if (hit.rigidbody){
Debug.Log("area effect objects");
}
}
}