Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by eric_c · May 21, 2011 at 12:07 PM · deathkillhealth

Detect different death sources?

Hi, I have a simple Health system for my little enemy.(When health <0 die) How do I detect different death types/last source that killed the enemy? Like if the enemy got killed by a bullet/explosion/fall or detecting who fired the shot that killed it? Is there some ways to do this by broadcasting messages or are there other ways of achieving this? Please help in JS, THANKS!

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
3
Best Answer

Answer by nhftk12 · May 21, 2011 at 12:52 PM

 var lives = 100;

  function OnTriggerEnter ( hit : Collider)
  {
      if (hit.gameObject.tag == "weapon1")
      {
          lives -= //add lives number;  // (can be more/less then the other bullets)
          
          Destroy(hit.gameObject);
          //do someting
          
          if (lives <= 0)
          {
              //dead
              print ("killed by weapon1");
          }
      }
      if (hit.gameObject.tag == "weapon2")
      {
          lives -= //add lives number;  // (can be more/less then the other bullets)
          Destroy(hit.gameObject);
          //do someting
          
          if (lives <= 0)
          {
              //dead
              print ("killed by weapon2");
          }
      }
      if (hit.gameObject.tag == "weapon3")
      {
          lives -= //add lives number;  // (can be more/less then the other bullets)
          
          Destroy(hit.gameObject);
          //do someting
          
          if (lives <= 0)
          {
              //dead
              print ("killed by weapon3");
          }
      }
  }

You add this code to the enemy and the enemy will act differantly according to the weapon/bullet that hit him. if his lives <= to 0, it will print who/what killed him.

Comment
Add comment · Show 2 · 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 eric_c · May 21, 2011 at 02:38 PM 0
Share

Thanks for the answer, did the job after some modifying!

avatar image nhftk12 · May 21, 2011 at 05:03 PM 0
Share

Good luke with your game!

avatar image
0

Answer by Ashkan_gc · May 21, 2011 at 12:16 PM

i know an efficent way that might not be as easy as you like. create a component called deathSource for all objects that can cause death and then in OnTriigerEnter /OnCollisionEnter that you check for death events take the reference of the object that is reducing your health and get it's deathSource component using GetComponent and for each object put it's information like name and deathtype in the component to retreive.

Comment
Add comment · Show 1 · 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 eric_c · May 21, 2011 at 12:25 PM 0
Share

I dont understand what you mean by checking for death events, can you explain it in detail or even post a code sample about what you're pointing against? Thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Sound effect when health=0? 2 Answers

PlayerHealth drops fast and wont reset 1 Answer

C# issue --- code assistance. 1 Answer

Death scene/screen on player Health Script Wont work! 0 Answers

health decrease only once and stop player movement 1 Answer


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