Advertisement
Guest User

Bk9iq - damage dealing

a guest
Jan 10th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*PlayerScript.js*/
  2.  
  3. var enemiesInRange:Array = new Array();
  4.  
  5. function OnTriggerEnter(other:Collider){ //Detect any object with a trigger Collider entering the trigger's radius
  6.     if(other.tag == "Player"){ //Check the tag set in Unity's inspector. If it's another player:
  7.          enemiesInRange.Add(other); //Add it to the list of enemies in range.
  8.     }
  9. }
  10.  
  11. //Now let's also make sure to clean up after they're out of range!
  12. function OnTriggerExit(other:Collider){ //Detect any object with a trigger Collider entering the trigger's radius
  13.     if(other.tag == "Player"){ //Or check if it's currently in the array.
  14.          enemiesInRange.Remove(other); //Remove it to the list of enemies in range.
  15.     }
  16. }
  17.  
  18. function Update(){
  19.     if(Input.GetButtonDown("YourAttackButton")){ //Attaaaaaaack!!
  20.         for(var enemy:Collider in enemiesInRange){ //Remember, we stored the other players' Colliders' only
  21.             var enemyScript:PlayerScript = col.GetComponent(PlayerScript); //Get the other player's PlayerScript (if that's what you called it).
  22.             enemyScript.hp -= 25; //deal 25 hp of damage. Alternatively I'd recommend using: enemyScript.loseHP(25);
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement