- Home /
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?
The enemy has a box collider with Trigger on. Btw if you want we can come skype or Team Viewer.
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.
Happy to help! Upvote and select this as answered so that others can see the result too :)
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.
Answer by Zuwolf · Jun 21, 2013 at 03:38 PM
Is your ennemy have the right tag ?
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.
Try to change "NoRequireReceiver" in "RequireReveiver". If your ennemy don't receive the message, an error will occured.
From what I discovered it doesn't even send the message.
You have to get the component script of col i think and send message to script not to collider.
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                