- Home /
 
The best way to damage an enemy (js)
I've got a game where I shoot a laser at enemy spaceships. Where is the best place for the script that damages the enemy?
I could do an OnCollisionEnter on the laser when it hits an enemy and access the enemies health, decrease it and destroy it if it reaches zero. But surely the bullet shouldn't be doing this? I could instead call a function on the enemy eg enemy.doDamage(damageAmt) which will figure out what to do when it's injured.
Or should I do the OnCollisionEnter on the enemy itself, checking for collision with lasers?
Answer by Kiwasi · Jul 24, 2014 at 01:50 PM
As a general principle entities should manage there own health. This tends to make code simpler to maintain as the entity knows what animations, sounds ect to call when damage is taken. This also allows effects to be applied simply, such as armour, invincibility, weakness.
It's generally easiest to do this with a public damage method. Call the damage method using send message for on the oncollision method of your bullet.
Ahh cool, I didn't know about Send$$anonymous$$essage. So I was sort of close when I gave the example of enemy.doDamage(damageAmt) - but actualy I'd use Send$$anonymous$$essage:
enemy.Send$$anonymous$$essage ("doDamage", damageAmt); - would this be about right? (not got Unity in front of me at the moment)
Send$$anonymous$$essage is slower, but works well if you have more or less then one method to call.
Calling the method directly will always be faster, but you have to make sure the method always exists. It also can't be used to call the same method on multiple scripts without knowing what each of those scripts are.
Ahh so we use Send$$anonymous$$essage because we're not certain the function exists? Got it.
Answer by Paprik · Jul 24, 2014 at 02:08 PM
Good thinking. It is much better to call a function on the Enemy class.
In my opinion, the best way to go about this is have an interface called something like IDamageable, that the Enemy would implement. This interface would have the DealDamage method you suggested with damageAmount and damageType parameters were that needed.
The implementation could then differ for different classes. You could consider the Enemy's armor in his implementation or consider if (IsIndestructible == false) in a world object etc.
You lost me at interface - I don't think you're talking about a GUI interface are you?
Google the unity interface tutorial it's pretty good. I am working form the phone, otherwise I'd post the link.
Another alternative to an interface is to use a Life component. I ended up using this approach as it's slightly simpler to code.
Interface is a general concept in object oriented program$$anonymous$$g. Link here. Classes that implement an interface have to implement all of its methods. But the implementation could be different in the different implementing classes.
I can expand the answer with example code if you want to.
Remember though I'm doing this in JS, not C# - so is this even an option to me?
Interfaces are available to JavaScript. Here is the unity tutorial. Even though the video is in C# there are links down the bottom to the same code in JavaScript.
Your answer
 
             Follow this Question
Related Questions
AirStrike Scripting Help 0 Answers
How to make Enemy shoot at Player Top Down 1 Answer
how to add firerate to enemies shooting script 0 Answers
Get enemy to fire at me from a high position 0 Answers
Rapid fire does damage every frame 0 Answers