- Home /
Link Health Bar and TNT Damage scripts together
I have a script that is attached to a car which gives is health and has a bar associated with it. I also have TNT which causes damage. I am not sure how to make the TNT subtract damage from the car health. Here is my explosion script which I found in a tutorial and changed to the way I thought would work. It has a bit to connect scripts but I am not sure the correct syntax to make it work-
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 == "Stockcar" || col.collider.gameObject.tag == "TNT"){
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);
Thanks
Answer by Hrethric · Mar 14, 2013 at 07:39 PM
You have to give at least one of the objects a RigidBody, and then do this:
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html
Depending on how you set up the logic, you will either need figure out what the car collided with, and decrease its health based on the result, or you will want to find what the TNT exploded, and do damage based on that.
I have been trying this but cannot figure out correct syntax, have shown the script now, so if you know what I need to change that would be great.
Hey, sorry I didn't see that you responded to this. I'm not sure if you figured it out or not, but you want to send the message you your collider's gameObject. I haven't used the message system extensively in Unity, but to my understanding that's the way it works.
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object 1 Answer
Health Bar Help 2 Answers
how do i make a script that calculate how much damage did i loss 1 Answer
For loop on collision (two hit kill) 2 Answers
Falling Damage 1 Answer