Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enemies and Units trading damage

Discussion in 'Scripting' started by lothop, Feb 20, 2015.

  1. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Hi,

    I currently have setup two units. A human unit who moves up and an enemy that moves sideways.

    They both have different ranges and the human unit has a higher range and stops on contact. The enemy moves so it is in line but wont do damage as its out of range.

    Each unit has its own stats, hp, dmg, attackspeed etc.
    coll.collider2D.gameObject.tag returns the enemies tag.

    How do I go about dealing damage to the enemy?
    Do I need to attach a takedamage and dodamage script to each game object?

    So " void OnTriggerEnter2D (Collider2D coll) " do damage*attackspeed to the enemy units current health.
    I'm unsure how to access the enemies health.
    The game objects have unitHealth attached to them which handles their current health.

    Would I do something like this?

    Code (csharp):
    1.  
    2. void OnTriggerEnter2D (Collider2D coll){
    3. enemy = coll.collider2D.gameObject.tag;
    4. unitHealth = enemy.GetComponent<unitHealth>();
    5. unitHealth.takeDamage(damage, attackSpeed);
    6. }
    Then in unitHealth script takeDamage to remove the hp?
     
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    There is only one problem I can see. The UnitHealth component
    will remain the same for all types of units.

    So you could either pass the type of unit to takeDamage and based on that cause
    the damage or else you could make an IHealth Interface and
    just implement that interface wherever you have to cause damage.

    Unity's scripting tutorial on Interface exactly explains such a health/damage
    system.
     
  3. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. private class HumanHealth : MonoBehaviour
    5. {
    6.      pubic float health;   //set this in the inspector
    7.      public float damage;  //set this in the inspector
    8.      public float attackSpeed;  //set this in the inspector
    9.  
    10.      void OnTriggerEnter2D(Collider col)
    11.      {
    12.             GameObject enemy = col.gameObject;
    13.             EnemyHealth enemyHealth = enemy.GetComponent<EnemyHealth>();
    14.             enemyHealth.TakeDamage(damage, attackSpeed); //damage the enemy
    15.             health -= enemyHealth.damage; //damage the player
    16.       }
    17. }
    this would go on the human unit for example;
     
  4. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    So doing it by tag will cause damage to all units with the same tag if collided with?
    I didn't quite understand how to implement an interface properly from the unity tutorial.

    Vintar, I will try your suggestion this morning
     
  5. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    I have it working. Issue is now the attackSpeed. It ramps up the " damageToUnit " really quickly. It gets to 200 in about 2 seconds doing 5 damage a tick.

    When the unit hits the enemy it does this.
    Code (csharp):
    1.  
    2. GameObject enemy = coll.gameObject;
    3. doDamage enemyScript = enemy.GetComponentInParent<doDamage>();
    4. enemyScript.damageUnit(damage, attackSpeed); //damage the enemy
    5.  
    Then attached to the enemy.
    Code (csharp):
    1.  
    2. public void damageUnit(int damage, float attackSpeed)
    3.     {
    4.         attackSpeed = attackSpeed;
    5.         damage = damage;
    6.         inRange = true;
    7.         if (health > damageToUnit) {
    8.             currentHealth = health - damageToUnit;
    9.         } else {
    10.             currentHealth = health;
    11.         }
    12.  
    13.     }
    14.  
    15. void Update () {
    16.         timer += Time.deltaTime;
    17.         if (timer >= attackSpeed && inRange && currentHealth > 0)
    18.         {
    19.            
    20.                 if (timer >= attackSpeed) {
    21.                     // Reset the timer.
    22.                     timer = 0f;
    23.                     if(currentHealth > damageToUnit)
    24.                     {
    25.                         // ... damage the enemy.
    26.                         damageToUnit += damage;
    27.                     }
    28.                 }
    29.            
    30.                 // If the unit has zero or less health...
    31.                 if (damageToUnit >= currentHealth) {
    32.  
    33.                     Debug.Log("dead");
    34.                 }
    35.        
    36.         }
    37.  
    38.     }
    It's not passing across damage and attackSpeed from void damageUnit
     
    Last edited: Feb 21, 2015
  6. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Solved by setting the variable within the enemy instead of running the function within the enemy.

    Code (csharp):
    1.  
    2. GameObject enemy = coll.gameObject;
    3.             doDamage enemyCombat = enemy.GetComponentInParent<doDamage>();
    4.             enemyCombat.damage = damage;
    5.             enemyCombat.attackSpeed = attackSpeed;
    6.