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 blad00x · Jan 06, 2013 at 03:58 PM · triggerdamagemeleecombat

Trigger and ennemy damage

Hello,

i want to make my player possibility to damage the ennemy and kill them so i make a script when ennemy touch player it hurt them.

HERE IS THE TRIGGER FUNCTION:

 function OnTriggerEnter ( other : Collider ) 
 {                       // trigger events for collider on foot
     print("Collision");
     if ( other.tag == "Ennemy")                                  
     {
     
         print("Ennemy Collision When attack");
         
         //Get component for modify health
        Ennemy = other.GetComponent ( EnnemyAI ); 
        
        //Define that ennemy near to player   
           ColEnemy = true;
     }
     else
     {
         ColEnemy = false;
     }
     
 }
 

AND HERE A SAMPLE OF DAMAGE (I USE ANOTHER WITH TOUCHPAD ECT... FOR ANDROID)

function Update() {

     //DEBUG TEST
     //If ennemy near to player
     if(ColEnemy)
     {
         //Modify health var of the ennemy
         Ennemy.enHealth -= 50;
         print(" Perte vie" + Ennemy.enHealth);
     }
 }
Comment
Add comment · Show 1
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 KiraSensei · Jan 06, 2013 at 04:56 PM 0
Share

So do you have a question or it was just a post to show your code to other people ?

2 Replies

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

Answer by $$anonymous$$ · Jan 06, 2013 at 05:00 PM

You should specify your question, because it isn't clear what's the problem you're having. Other than that, what I already see as problems are these:

1) In Update() method, you should set ColEnemy = false after you apply damage, because this way 50 damage will be applied in every frame, which makes your enemy die instantly.

2) Also, in an OnTriggerExit(), you should set ColEnemy = false, so your enemy can actually save itself from the player once it entered your player's damaging radius, by moving out of it.

3) This code will only work well for exactly ONE enemy. The reason for this is if multiple enemies collide with the player simultaneously, this code still only applies damage to one of them.

In general I say that the way you coded this is not very good programming practice. To make it better:

a) Move the damage code from Update() to OnTriggerEnter(). This also eliminates the need for the ColEnemy variable, and you can actually damage every enemy which triggers the player.

b) If you'd like your enemies to take damage over time, you can also move the damage code from OnTriggerEnter() to OnTriggerStay(). This will be called every frame that the enemy is actually in collision with your player. OnTriggerStay() of course already means that an OnTriggerEnter() happened, so you don't have to check that if you don't want to.

c) The above means that your enemies will be damaged every frame, so let's move the damage code again, now to EnnemyAI's OnTriggerStay(). Make a function takeDamage(damage : int), and check here if enough time has passed since the last application of damage with the help of a damageInterval : float and secondsSinceLastDamage variable, which control how much seconds need to pass between damaging:

 function OnTriggerStay(collider : Collider)
 {
   if (collider.gameObject.tag == "Player")
   {
      takeDamage(50);       
   }
 }
 
 function takeDamage(damage : int)
 {
   if (secondsSinceLastDamage > damageInterval)
   {
      ActuallyApplyDamageAndCheckIfKilled(damage);
      secondsSinceLastDamage = 0;
   }
 }


Also append this to your EnnemyAI Update(): secondsSinceLastDamage += Time.deltaTime. ActuallyApplyDamageAndCheckIfKilled(damage : int) is just a placeholder here, so make a function for this or just write the code in place here, to subtract from enHealth and if the enemy died, destroy it with Destroy(gameObject), and call any additional game logic here.

Still this code can be further polished, for example to query the player (or some game logic script) as to how much damage should be applied, and not hardcode it to 50, but since you haven't actually asked a question, this is pretty much already so far I've gone with this!

Comment
Add comment · 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
0

Answer by blad00x · Jan 06, 2013 at 07:26 PM

Thanks you OnTriggerStay was exactly what i was looking for. i modify my script now it's smaller and work well.

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 AlucardJay · Jan 06, 2013 at 07:26 PM 1
Share

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]

Under the answer where it says edit | delete | more , click on more , then convert to comment


How to accept an answer :

On the left-hand-side of the Answer box , there are the following icons :

 Thumb Up  
 Number (of votes) 
 Thumb Down 
 A Tick 

If an answer worked for you , click on the 'Tick' , the answer should now be highlighted in green. If you like an answer on Any question , you can click on the Thumb UP , the thumb should now be highlighted in green , and the number of votes should rise by 1.

How to reply to an answer / post a comment :

To make a comment , USE the [add new comment] button, a window then opens to type in. The answer fields are for ANSWERS, so unless you are answering your own question , DON'T write in an answer box. This helps the 'site work properly, especially when other people are searching for answers, and want to read answers , not comments.

IF your question changes slightly while commenting and reading comments , EDIT the original Question, so anyone reading from the beginning knows what you are asking.

This will make for a happy experience for everyone. I made mistakes starting on this 'site too, but everyone is helpful if you learn and change these habits.

Following these simple steps helps the website work , and other readers to find answers also.

Happy Coding =]

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

10 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

Related Questions

Gameobject not registering collisions 1 Answer

Distance will not calculate after button is pressed 0 Answers

Melee Combat Collision 2 Answers

How do I make my melee script do damage. 1 Answer

Setting Input windows or queuing user input 2 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