public void Explode() { Vector3 explodePos = explosive.transform.position; // grab position of gameobject Collider[] colliders = Physics.OverlapSphere(explodePos, radius); // overlapsphere, any gameobject that has a collider, will be put in this array foreach (Collider hit in colliders) // want to get component of each collider { MasterBody player = hit.gameObject.GetComponent <MasterBody>(); GameObject GO = hit.transform.gameObject; // knockback thanks to the HammerAndSickle/MasterBody class if (player != null) { float distance = Vector3.Distance(transform.position, player.transform.position); // position between explosive and player // If the player is far from the explosion belt, it receives less damage and vice versa. float farDmg = 2; float closeDmg = 5; if (distance == farDmg) { player.TakeDamage(transform.gameObject, 2.0f, 10f, (GO.transform.position - explosive.transform.position).normalized + new Vector3(0, (-1) - (GO.transform.position - explosive.transform.position).normalized.y, 0)); } if (distance == closeDmg) { player.TakeDamage(transform.gameObject, 5.0f, 10f, (GO.transform.position - explosive.transform.position).normalized + new Vector3(0, (-1) - (GO.transform.position - explosive.transform.position).normalized.y, 0)); } } } // Instantiate the particle effect upon hit. Code is from deathEffect particle (masterbody). particleObject = Instantiate(explodeEffect); particleObject.transform.position = transform.position; }
// when the player attacs public override void Fire() { // test if cooldown is over since last attack if (currentCooldown > Time.timeSinceLevelLoad) { return; } currentCooldown = Time.timeSinceLevelLoad + cooldown; //used for animation attack = true; //get every body in the attack area; foreach (Collider col in attackBox.collider) { //damaging the wall in the player confirm part of the game if (col.tag == "DestroyableWall") { getHit targetGetHit = col.GetComponent <getHit>(); targetGetHit.health -= damage; targetGetHit.GetComponent <AudioSource>().Play(); //Plays the get hit sound effect. } GameObject GO = col.transform.root.gameObject; if (GO.tag == "Player") // if a player is hit { MasterBody EnemyMb = GO.GetComponent <MasterBody>(); MasterBody mb = gameObject.transform.root.GetComponent <MasterBody>(); // get playerdata to later divide the coins playerStats.playerData enemy = pS.players.Find(x => x.playerNr.Equals(EnemyMb.playerID)); // get the stat from the enemy playerStats.playerData player = pS.players.Find(x => x.playerNr.Equals(mb.playerID)); // get stats from self int enValue = (player.coins - enemy.coins) / 2; // determine how many coins the enemy gets int plValue = (enemy.coins - player.coins) / 2; // determine how many coins the player gets // add the determined amound coins to each player pS.addCoin(EnemyMb.name, enValue); pS.addCoin(mb.name, plValue); //give the enemy some damage and knockback EnemyMb.TakeDamage(gameObject.transform.root.gameObject, damage, knockBack, (GO.transform.position - transform.position).normalized + new Vector3(0, (-1) - (GO.transform.position - transform.position).normalized.y, 0)); } } Shoot(); // play attacking sound }