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
1
Question by boriswc97 · Jun 21, 2013 at 03:22 PM · enemydamageattackmeleehealth

Melee Damage script by collision

Hi, I'm working on a 2D game and I have been having some problems with melee damage by collision, I made this script:

public class AttackScript : MonoBehaviour {

 public float damage = 50.0F;
 public float attackDuration = 0.3F;
 
 public bool attacking = false;

 
 [HideInInspector]
 
 
 void Start () 
 {
     
 }
 
 void Update()
 {
     
     
     if(Input.GetKeyDown("h")){
         attacking = true;
     }
 }
 
 void OnTriggerEnter (Collider col) 
 {
 
     if(col.tag == "Enemy")
     {
         if(attacking)
         {
             col.SendMessage("receiveDamage", damage ,SendMessageOptions.DontRequireReceiver);
         }
     }
 }
 
 void EnableDamage()
 {
     if (attacking == true) return;
     attacking = true;
     StartCoroutine("DisableDamage");
 }
 
 IEnumerator DisableDamage()
 {
     yield return new WaitForSeconds(attackDuration);
     attacking = false;
 }

}

And the enemy's health script:

public class EnemyHealth : MonoBehaviour {

 public float maxHP = 100.0F;
 public float currentHP;

 public GameObject Enemy;

 // Use this for initialization
 void Start () {
     
     currentHP = maxHP;
 
 }
 
 // Update is called once per frame
 void Update () {
     
     checkStatus();
 
 }
 

public void checkStatus(){

     if(currentHP > maxHP)
         currentHP = maxHP;
     
     if(currentHP < 0)
         currentHP = 0;
     
     if(currentHP == 0)
         death();
 }
 
 public void receiveDamage(float damage){
     
     currentHP -= damage;
     Debug.Log("Damage Applied");
 }
 
 public void death(){        
 
     Destroy(this.gameObject);        
     
 }
 

}


But for some reason it doesn't work, I tried a lot of things and it doesn't work, some help please?

Comment
Add comment · Show 5
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 Zuwolf · Jun 21, 2013 at 03:38 PM 0
Share

Is your ennemy have the right tag ?

avatar image boriswc97 · Jun 21, 2013 at 04:16 PM 0
Share

Nothing :/

avatar image boriswc97 · Jun 21, 2013 at 04:19 PM 0
Share

What do you mean?

avatar image boriswc97 · Jun 21, 2013 at 04:21 PM 0
Share

The enemy has a box collider with Trigger on. Btw if you want we can come skype or Team Viewer.

avatar image boriswc97 · Jun 21, 2013 at 04:24 PM 0
Share

The sword owns the script, and it has a box collider

2 Replies

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

Answer by brandonsbarber · Jun 21, 2013 at 04:29 PM

Whenever using Triggers, make sure that one of the Objects that will be hitting has a RigidBody component attached. You can zero out the values if you want to (Note: mass will never be zero because all objects must have mass. It's a physics thing).

This should allow the Trigger to work properly.

Comment
Add comment · Show 12 · 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 boriswc97 · Jun 21, 2013 at 04:32 PM 0
Share

Thank you, now it works!

avatar image brandonsbarber · Jun 21, 2013 at 04:33 PM 0
Share

Happy to help! Upvote and select this as answered so that others can see the result too :)

avatar image boriswc97 · Jun 21, 2013 at 04:35 PM 0
Share

Doesn't let me Upvote O.o

avatar image brandonsbarber · Jun 21, 2013 at 04:36 PM 0
Share

It's because of your "karma" level. You need a 15 to be able to upvote, I believe. No worries :) just glad I was able to help.

avatar image boriswc97 · Jun 21, 2013 at 04:38 PM 0
Share

Oh thank you, and yeah thanks for all the help

Show more comments
avatar image
0

Answer by Zuwolf · Jun 21, 2013 at 03:38 PM

Is your ennemy have the right tag ?

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 boriswc97 · Jun 21, 2013 at 03:39 PM 0
Share

Yes Indeed

avatar image Zuwolf · Jun 21, 2013 at 03:42 PM 0
Share

Try to find, why it doesn't work with Debug.log Put it to know col.tag state, attacking state, col.Send$$anonymous$$essage state to know where there is a problem.

avatar image Zuwolf · Jun 21, 2013 at 03:47 PM 0
Share

Try to change "NoRequireReceiver" in "RequireReveiver". If your ennemy don't receive the message, an error will occured.

avatar image boriswc97 · Jun 21, 2013 at 03:56 PM 0
Share

From what I discovered it doesn't even send the message.

avatar image Zuwolf · Jun 21, 2013 at 04:08 PM 0
Share

You have to get the component script of col i think and send message to script not to collider.

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

16 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

Related Questions

Object Not Recieving Damage 3 Answers

Health below zero but shows negative numbers 1 Answer

Problem with enemy health. 1 Answer

One enemy triggers all the enemies 2 Answers

rigidbody enemy goes only forward, no damage delay. 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