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 Conect11 · Dec 12, 2013 at 09:41 PM · ontriggerenterdamagecs

Specify which trigger does what

EDIT: Suitably humbled, I'm adding the code below. EDIT2: Strange, even with no weapon, and therefore no way to take damage, the npc still loses hp, playing the audio sound, and eventually dying. I'm confused. Here it is: I have a troll npc who walks toward the player, and holds a club, used for attacking. There is a box collider with a trigger attached and a script (on the club, which is attached to the troll's armature) which causes damage to the player on trigger enter. Everything's cool there. Separately, I have attached a health script for the troll on the parent object, with a different box collider and trigger attached. Now, in my noob brain I'd imagine that if the club hit me (looking for player tag in ontriggerenter) I'd take damage, which I do, but so does the npc, even though the two box colliders do not overlap. And so I'm wondering if I should go about things a different way, or maybe simply add an "attack" variable to the attack component which temporarily stops the npc from taking damage? Thanks for any and all feedback. God bless.

 using UnityEngine;
 using System.Collections;
 
 public class GolgorLife : MonoBehaviour{
 
 
 
     
     
     
     float enemyHealth;
     bool enemyDeadBool;
      
      
     
     void Start()  {
     {
     enemyHealth = 199;
     enemyDeadBool = false;
     }
     }
     
     void OnTriggerEnter(Collider triggerObject)  {
     {
      
     //assuming the sword object is named sword
     if (triggerObject.gameObject.name == "sword")
     {
     enemyHealth += - 25;
     audio.Play();        
     }
     else 
     {
     if (triggerObject.gameObject.name == "club")
     {
         enemyHealth += - 10;
     audio.Play();
 }
             }
     }
     } 
     void Update()
     {
     if (enemyHealth <= 0)
     {
         
     //may be useful to not just instantly destroy
     //you can add +experience or death animation etc...        
     //animation.Play("die");            
     enemyDeadBool = true;
     }
      
     if (enemyDeadBool == true)
     {
     //do anything to reward player or death animations etc...
     
     
     Destroy(gameObject,5);
                 }
         
             }
     }
Comment
Add comment · Show 3
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 tanoshimi · Dec 12, 2013 at 10:56 PM 1
Share

Who's got the club/sword - you or the enemy? And who's got this script attached to them? It sounds like the enemy is detected a collision with their own weapon, which would cause the health drop/audio you describe.

avatar image rednax20 · Dec 12, 2013 at 11:01 PM 0
Share

it looks like your colliders might be overlapping anyway. I can't really grasp what you are trying to do, although i don't know c# so that could easily be why it looks so messed up to me.

if you haven't already, try putting the objects on different layers and set their layer collision matrix to not collide with each other.

(you can find the layer collision matrix under project settings physics)

good luck!

avatar image Conect11 · Dec 12, 2013 at 11:12 PM 0
Share

Thankstanoshimi, both the player and npc have a club, that makes sense! Will check when I get home. Thanks!

EDIT: It worked!!!!! I love when the simple answer is the best one. :)

If you convert that to an answer I'll be happy to accept it :)

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Rocket projectile damage not applied on collision (message not sent/received) 1 Answer

Damage rate HELP 1 Answer

Area of Damage for a Blink of an Eye 3 Answers

how to make a character flash red when damaged 1 Answer

How do I apply damage to the enemy ai with projectile 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