- Home /
TNT Damage Receiver
Hi everyone, i've got a problem with my damage receiver script.
How to make all players, AI, and objects got hit / applied damage if they were in the TNT (TNT has explode all objects will affected by the explosion on certain range, i don't know how to explain (Damage between range))
How to make if the player / bullets touched the TNT, the TNT Starts counting down (Beeping fast) till 3 seconds then boom.
I attached my scripts here thanks for the help! :D
enum ExplosionTypes {TNT, Nitro, Other}
var explosionType : ExplosionTypes;
var explosion : Transform;
var damage : float;
var hitPoints : float = 0;
var deadReplacement : Rigidbody;
var explosionFX : AudioClip;
private var explodeSecs : float = 1;
private var duration : float = 1;
private var isTNT = false;
function OnCollisionEnter (col : Collision){
var contact : ContactPoint = col.contacts[0];
var rot : Quaternion = Quaternion.FromToRotation(Vector3.up,contact.normal);
var pos : Vector3 = contact.point;
if(col.collider.gameObject.tag == "Player" || col.collider.gameObject.tag == "Bullets"){
if(explosionType == ExplosionTypes.Nitro){
Instantiate(explosion,pos,rot);
Explode();
audio.PlayOneShot(explosionFX);
Destroy(gameObject);
col.collider.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
}
if(explosionType == ExplosionTypes.TNT){
isTNT = true;
}
if(explosionType == ExplosionTypes.Other){
Instantiate(explosion,pos,rot);
Explode();
audio.PlayOneShot(explosionFX);
Destroy(gameObject);
col.collider.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
}
}
}
function Update () {
var t : float = Mathf.PingPong (Time.time, duration) / duration;
if(isTNT){
renderer.material.color = Color.Lerp(Color.red,Color.gray,t);
}
}
function Explode(){
var expl : Transform;
expl = Instantiate(explosion,transform.position, transform.rotation);
Destroy(gameObject);
if(isTNT) isTNT = false;
}
@script RequireComponent(AudioSource);
Answer by suruz@kento · Dec 02, 2011 at 04:59 PM
Solution to the first one is relatively easy. Just Modify Explode() so that it can determine its explotion effect rang. If am not wrong you are sending damage message to reciver using SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver). In that case using it with in Explode() will conform better reusability.
var radius: float = 100; // explotion radius
function Explode() {
var expl : Transform;
expl = Instantiate(explosion,transform.position, transform.rotation);
Destroy(gameObject);
if(isTNT) isTNT = false;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
for (var hit in colliders)
{
//you do'nt want to count TNT itself as hit terget
if(hit.name == name) return;
print("explotion hit:" + hit.name);
hit.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
//if you want to add explotion force
var body: Rigidbody =hit.gameObject.rigidbody;
if(body)
{
body.AddForce(Vector3.up* 150);
}
}
}
When you use this function you do'nt need to call SendMessage() and Destroy() after it.
You can use Invoke(handlerFunc, timeout) as a solution to the second one. you have timeout of 3 secs.
var BeepingSound: AudioClip;
var TimeOut: float = 3; //adjus as your requirement
function HandlerFunc() { audio.Stop(); Explode(); audio.PlayOneShot(explosionFX); }
function PlayBeeps() {
audio.clip = BeepingSound; //fresh clip
audio.Play();
Invoke("HandlerFunc",TimeOut);
}
call PlayBeeps() in place of Explode() in function OnCollisionEnter ()
function OnCollisionEnter (col : Collision){ var contact : ContactPoint = col.contacts[0]; var rot : Quaternion = Quaternion.FromToRotation(Vector3.up,contact.normal); var pos : Vector3 = contact.point;
if(col.collider.gameObject.tag == "Player" || col.collider.gameObject.tag == "Bullets"){
if(explosionType == ExplosionTypes.Nitro){
Instantiate(explosion,pos,rot);
//Explode();
PlayBeeps();
}
if(explosionType == ExplosionTypes.TNT){
isTNT = true;
}
if(explosionType == ExplosionTypes.Other){
Instantiate(explosion,pos,rot);
//Explode();
PlayBeeps() ;
}
}
}
thanks it's work!! anyway what is explosionPos ? I use explosionPos : Vector3 = Vector3.up * 10;
Sorry I missed that declaration. It will be var explosionPos : Vector3 = transform.position;