Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*PlayerScript.js*/
- var enemiesInRange:Array = new Array();
- function OnTriggerEnter(other:Collider){ //Detect any object with a trigger Collider entering the trigger's radius
- if(other.tag == "Player"){ //Check the tag set in Unity's inspector. If it's another player:
- enemiesInRange.Add(other); //Add it to the list of enemies in range.
- }
- }
- //Now let's also make sure to clean up after they're out of range!
- function OnTriggerExit(other:Collider){ //Detect any object with a trigger Collider entering the trigger's radius
- if(other.tag == "Player"){ //Or check if it's currently in the array.
- enemiesInRange.Remove(other); //Remove it to the list of enemies in range.
- }
- }
- function Update(){
- if(Input.GetButtonDown("YourAttackButton")){ //Attaaaaaaack!!
- for(var enemy:Collider in enemiesInRange){ //Remember, we stored the other players' Colliders' only
- var enemyScript:PlayerScript = col.GetComponent(PlayerScript); //Get the other player's PlayerScript (if that's what you called it).
- enemyScript.hp -= 25; //deal 25 hp of damage. Alternatively I'd recommend using: enemyScript.loseHP(25);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement