Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by daviddickball · Jul 24, 2014 at 01:43 PM · enemyshooting

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daviddickball · Jul 24, 2014 at 02:17 PM 0
Share

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)

avatar image Kiwasi · Jul 24, 2014 at 02:36 PM 0
Share

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.

avatar image daviddickball · Jul 24, 2014 at 03:33 PM 0
Share

Ahh so we use Send$$anonymous$$essage because we're not certain the function exists? Got it.

avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daviddickball · Jul 24, 2014 at 02:13 PM 0
Share

You lost me at interface - I don't think you're talking about a GUI interface are you?

avatar image Kiwasi · Jul 24, 2014 at 02:28 PM 0
Share

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.

avatar image Paprik · Jul 24, 2014 at 02:49 PM 0
Share

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.

avatar image daviddickball · Jul 24, 2014 at 03:36 PM 0
Share

Remember though I'm doing this in JS, not C# - so is this even an option to me?

avatar image Kiwasi · Jul 24, 2014 at 05:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges