Search Unity

Passing collision information to Update()

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

  1. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Hi,

    I currently have two objects colliding and taking damage from each other. When one dies and the other starts moving again it also keeps taking damage.

    On collision
    Code (csharp):
    1.  
    2. damage = unitStats.damage; // Damage to send to enemy to take
    3. attackSpeed = unitStats.attackSpeed; // attackspeed to send to enemy to take
    4. GameObject enemy = coll.gameObject;
    5. initiateCombat enemyCombat = enemy.GetComponentInParent<initiateCombat>();
    6. enemyCombat.dealDamage = damage; // Tell enemy how much damage to take
    7. enemyCombat.dealDamageSpeed = attackSpeed; // Tell enemy how fast to take it
    8. enemyCombat.enemyInRange = true; // Tell enemy that its in range
    9. enemyCombat.startCombat = true; // Tell enemy to start taking damage
    10.  
    Then on the update()
    Code (csharp):
    1. if (damageToUnit >= currentHealth) { Destroy (gameObject); }
    What I need to get this all to work is for before / after the destroy, it tells the colliding object to stop doing damage.
    So
    Code (csharp):
    1. enemyCombat.enemyInRange = false; // Tell enemy that its not in range
    2. enemyCombat.startCombat = false; // Tell enemy to stop taking damage
    But I cant access the enemyCombat within Update()
    I tried making it so when the unit moves again it isnt in range / isnt in combat but units with different ranges stops all damage being done.
    If this can't be done this way please let me know.
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  3. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Because the unit is destroyed with Destroy(gameObject); onCollisionExit does not proc.
     
  4. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Solved.

    Code (csharp):
    1.  
    2. void Update () {
    3. bool isAttacking = (enemy != null);
    4.         if(!isAttacking)
    5.         {
    6.             enemyInRange = false; // Tell unit to disengage combat
    7.             startCombat = false; // Tell unit to disengage combat
    8.         }
    9. }
    10.  
     
  5. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    for giggles, this will do the same job without needing the variable.

    Code (CSharp):
    1.     void Update() {
    2.         if (!enemy) {
    3.             enemyInRange = false; // Tell unit to disengage combat
    4.             startCombat = false; // Tell unit to disengage combat
    5.         }
    6.     }
     
  6. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    This only works for units with long range. Short range vs long range doesnt work. So on connection the long range unit sets enemyCombat.enemy = gameObject; on the short range unit which meets all conditions to take damage.

    I will change it to that, thanks kdub :) << Works fine :)

    Now I need to handle two units attacking one...
    I guess instead of the unit " doing damage to itself " send the damage via enemyCombat.dealDamage= damage;