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

Box Collider Technical Difficulties

Discussion in 'Scripting' started by saif_youssef, Jul 10, 2014.

  1. saif_youssef

    saif_youssef

    Joined:
    Jul 10, 2014
    Posts:
    5
    Hello:
    I'm very new to Unity and Javascript... I've had difficulties, but i always manage to get it to work... However, I am trying to have a Tank fire at an area that contains a Target... Which is now working perfectly, but what im trying to do is take the variable damage from the tank.. and applying that to any "enemies" in that area... My plan is to make a box collider and have that spawn at the target location, and SOMEHOW take the variable Damage from the UnitInfo script of the attacker, and applying that in the UnitInfo script of the receiever...
    Code (JavaScript):
    1. private var UnitInfoInstance : UnitInfo;
    2. var Damage : float;
    3.  
    4.  
    5. function Update () {
    6.  
    7. }
    8.  
    9. function OnCollisionEnter(collision : Collision)
    10. {
    11.     UnitInfoInstance = GetComponent(UnitInfo);
    12.     var Damage = UnitInfoInstance.Damage;
    13.     SendMessage("ApplyDamage", Damage);
    14. }
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    use an OverlapSphere at the point of impact. then send the message to all enemies inside the sphere.
     
    saif_youssef likes this.
  3. saif_youssef

    saif_youssef

    Joined:
    Jul 10, 2014
    Posts:
    5
    Thank you kind Sir,
    I've researched it, and finally got a working function. Now my idea seems to work, for the most part... Problem is, when the Damage is applied, it's applied OVER and OVER in the same target... I want it to apply the damage once Per Target... Here is my code:

    Code (JavaScript):
    1.     var cols: Collider[] = Physics.OverlapSphere(Target.transform.position, DamageRadius);
    2.  
    3.     for (var col: Collider in cols)
    4.     {
    5.         var script: UnitInfo;
    6.         script = col.GetComponent(UnitInfo);
    7.         script.CurrentHealth -= UnitInfoInstance.Damage;
    8.     }
    ** I'm pretty sure it has something to do with the for loop, i'll keep experimenting with it, maybe i get to something
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Set your array to null after you have applied the damage to all the units
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    do you still have this snippet inside the projectile's OnCollisionEnter function? because then it should only do it once since the enter is only detected once.