- Home /
Damage/Health problem
All right, I've been looking and testing all sorts of examples and answers, the problem is simple yet I fail to get it right.
I have a small prop (a barrel) and I want to shoot it with a projectile and kill it after 2 hits (for the arguments' sake)
Now, for the prop I have this from the fps example:
var hitPoints = 100.0; var detonationDelay = 0.0; var deadReplacement : GameObject;
function ApplyDamage (damage : float) {
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
Invoke("DelayedDetonate", detonationDelay);
}
}
function DelayedDetonate () { BroadcastMessage ("Detonate"); }
function Detonate () {
Destroy(gameObject);
if (deadReplacement) {
var dead : GameObject = Instantiate(deadReplacement, transform.position, transform.rotation);
}
}
and for the projectile, this:
var explosion : GameObject;
function OnCollisionEnter( collision : Collision ){ var contact : ContactPoint = collision.contacts[0]; var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal ); var instantiatedExplosion : GameObject = Instantiate(explosion, contact.point, rotation ); if (collision.gameObject.tag=="PreciousWreckageBarrels") SendMessage("ApplyDamage",5.0,SendMessageOptions.DontRequireReceiver); } function FixedUpdate () { rigidbody.AddForce (transform.TransformDirection (Vector3.forward) * 6000.0); }
Tagged the prop correctly, did everything in my humble powers. Should I stop scripting? :)
Any help, greatly appreciated.
Answer by Jean-Fabre · May 03, 2011 at 02:42 PM
Ok,
Not sure exactly where is the problem, but the best answer is to give you some hints on how you can find the problem:
1: either in the debugger or with some good old Debug.Log("hello"). try to progress through the system.
-- do you reach OnCollisionEnter ( that is if you put Debug.Log("hello") as the first instruction in OnCollisionEnter and you do get it in the console when you see the projectile hit your object)
-- if yes, do you reach ApplyDamage() ?
-- if yes, does hitPoints get properly set?
-- if yes, do you reach DelayedDetonate ?
-- if yes, then all is fine
By doing so, you'll be able to locate the source of your problem accuratly.
Else, can you explain what exactly is not working?
Bye,
Jean
Answer by Rabwin · May 03, 2011 at 02:51 PM
First of all, it doesn't need to be a broadcast, otherwise you might end up blowing up other barrels too. You should be using something like the following:
this.sendmessage("Detonate");
better
Detonate();
Also, does it really need 6000 force? Your scene scale must be huge O.o
IMO the object is moving so fast, it actually goes through the barrel on each frame. I reccomend doing a Debug.Log("something") at each section to find out whos the culprit.
Good luck!
EDIT: lol jean beat me to it.