Search Unity

Collision within GameObject

Discussion in 'Scripting' started by MonkeyKasai, Oct 2, 2014.

  1. MonkeyKasai

    MonkeyKasai

    Joined:
    Jan 4, 2014
    Posts:
    40
    Hey guys,
    In my game I have it so when you push a button a cube appears in front of the player. I want to make it so that al the enemies within that cube take damage.

    This is what I tryed doing.

    Code (JavaScript):
    1. var Attack : GameObject;
    2. var Timer : float = 0.1;
    3. var DestroyAttack = false;
    4.  
    5. function OnCollisionStay(collision: Collision) {
    6.  
    7.     if(collision.gameObject.tag == "Enemy"){
    8.         var Enemy = collision.GetComponent(EnemyStats);
    9.         Enemy.Health -= 250;
    10.         DestroyAttack = true;
    11.     }
    12. }
    13.  
    14. function Update(){
    15.  
    16.     Timer -= Time.deltaTime;
    17.    
    18.     if(Timer < 0){
    19.         DestroyAttack = true;
    20.     }
    21.  
    22.     if(DestroyAttack == true){
    23.         Destroy(Attack);
    24.     }
    25. }

    I tryed get physics.overlapsphere to work but I have no clue what to do haha