IEnumerator ExplodeMine(ShipHandling shipHandling, BulletCollision bc, float waitForSeconds = 0) { yield return(new WaitForSeconds(waitForSeconds)); Collider[] hitColliders = Physics.OverlapSphere(transform.position, radius); List <ShipHandling> shipsInArea = new List <ShipHandling>(); int i = 0; while (i < hitColliders.Length) { //Debug.Log(hitColliders[i].GetType() + " " + hitColliders[i].transform.parent.tag); if (hitColliders[i].transform.parent != null && hitColliders[i].transform.parent.tag == "CameraObject") { if (shipsInArea.IndexOf(hitColliders[i].transform.GetComponent <ShipHandling>()) < 0) { shipsInArea.Add(hitColliders[i].transform.GetComponent <ShipHandling>()); //hitColliders[i].transform.GetComponent<Renderer>().GetComponent<Material>().color = Color.yellow; } } i++; } //Debug.Log("shipsInArea = "+shipsInArea.Count); //Do the damage to all players in the area i = 0; foreach (ShipHandling currShipHandling in shipsInArea) { bool shipDestroyed = shipHandling.DoDamage(bc.damage); if (shipDestroyed == true) { GameObject playerStats = GameObject.FindGameObjectWithTag("Player1Stats"); // Don't add points if killed by itself! if (bc.bulletOwnerPlayerNumber != shipHandling.playerNumber) { playerStats.GetComponent <UpdatePlayerStats>().playerScores[(int)bc.bulletOwnerPlayerNumber - 1]++; } } } // Do the big explosion GameObject explosion = Instantiate(bc.explosionPrefabBig, transform.position, Quaternion.identity); Destroy(explosion, 3.0f); Destroy(this.gameObject); Destroy(transform.parent.gameObject); }
void OnCollisionEnter(Collision col) { ShipHandling shipHandling = col.gameObject.GetComponent <ShipHandling>(); //Debug.Log("OnCollisionEnter"); // Check for own deployed mines first if (col.gameObject.tag == "CameraObject" && isMine == true && bulletOwnerPlayerNumber == shipHandling.playerNumber) { // Do nothing, just push it on collision. } else { if (col.gameObject.tag == "CameraObject") { bool shipDestroyed = shipHandling.DoDamage(damage); GameObject explosion; if (isMine) { explosion = Instantiate(explosionPrefabBig, transform.position, Quaternion.identity); } else { explosion = Instantiate(explosionPrefab, transform.position, Quaternion.identity); } //GameObject explosion = Instantiate(explosionPrefab, transform.position, Quaternion.identity); Destroy(explosion, 3.0f); Destroy(this.gameObject); if (shipDestroyed == true) { GameObject playerStats = GameObject.FindGameObjectWithTag("Player1Stats"); //Debug.Log("Killer was " + bulletOwnerPlayerNumber); // Don't add points if killed by itself! if (bulletOwnerPlayerNumber != shipHandling.playerNumber) { playerStats.GetComponent <UpdatePlayerStats>().playerScores[(int)bulletOwnerPlayerNumber - 1]++; } } } else if (col.gameObject.tag == "Bullet") { //Debug.Log("Bullet Collision!!"); GameObject explosion; // Exchange damage bulletHitPoints = bulletHitPoints - col.transform.GetComponent <BulletCollision>().damage; col.transform.GetComponent <BulletCollision>().bulletHitPoints = col.transform.GetComponent <BulletCollision>().bulletHitPoints - damage; //Debug.Log("Bullet hitpoints after collision: " + bulletHitPoints + " and " + col.transform.GetComponent<BulletCollision>().bulletHitPoints); // Check for casualties if (bulletHitPoints <= 0) { if (isMine) { explosion = Instantiate(explosionPrefabBig, transform.position, Quaternion.identity); } else { explosion = Instantiate(explosionPrefab, transform.position, Quaternion.identity); } Destroy(explosion, 3.0f); Destroy(this.gameObject); } if (col.transform.GetComponent <BulletCollision>().bulletHitPoints <= 0) { if (col.transform.GetComponent <BulletCollision>().isMine) { explosion = Instantiate(explosionPrefabBig, transform.position, Quaternion.identity); } else { explosion = Instantiate(explosionPrefab, transform.position, Quaternion.identity); } Destroy(explosion, 3.0f); Destroy(col.gameObject); } } else { // On any other object, just destroy the bullet GameObject explosion = Instantiate(explosionPrefab, transform.position, Quaternion.identity); Destroy(explosion, 3.0f); Destroy(this.gameObject); } } }
public void RedrawShip17Primary() { ShipHandling shipHandling = this.GetComponentInParent <ShipHandling>(); LineRenderer laserBeamRenderer = this.GetComponentInChildren <LineRenderer>(); ShipDetails shipDetails = shipHandling.shipDetails; List <Transform> bulletPrimarySpawnPoints = new List <Transform>(); int i = 0; foreach (Transform child in transform) { if (child.CompareTag("BulletSpawn") && child.name.Contains("BulletSpawnPointPrimary1")) { bulletPrimarySpawnPoints.Add(child.transform); i++; } } List <Transform> usedSpawnPoints = new List <Transform>(); usedSpawnPoints = bulletPrimarySpawnPoints; foreach (Transform currBulletSpawnPoint in usedSpawnPoints) { RaycastHit hit; laserBeamRenderer.enabled = true; laserBeamRenderer.SetPosition(0, currBulletSpawnPoint.position); Vector3 direction = currBulletSpawnPoint.transform.forward; // Reduce laser length according to ship speed Rigidbody rb = GetComponent <Rigidbody>(); Vector3 vel = rb.velocity; float finalLaserLength = Ship17LaserLength - vel.magnitude; //Debug.Log("finalLaserLength = " + finalLaserLength); if (finalLaserLength < Ship17LaserMinLength) { finalLaserLength = Ship17LaserMinLength; } Vector3 endPoint = currBulletSpawnPoint.transform.position + currBulletSpawnPoint.transform.forward * finalLaserLength; Vector3 fwd = currBulletSpawnPoint.transform.TransformDirection(Vector3.forward); if (Physics.Raycast(currBulletSpawnPoint.transform.position, fwd, out hit, finalLaserLength)) { //print("There is something in front of the object! " + hit.distance); endPoint = hit.point; ShipHandling hitShipHandling = hit.collider.gameObject.GetComponentInParent <ShipHandling>(); // Do damage if (Ship17LaserDrawCount >= Ship17LaserDoDamageInterval) { // Don't do damage every time laser is drawn GameObject instance = Resources.Load("Prefabs/ShrapnelExplosionMedium") as GameObject; GameObject explosion = Instantiate(instance, hit.point, Quaternion.identity); Destroy(explosion, 3.0f); Ship17LaserDrawCount = 0; if (hitShipHandling != null) { hitShipHandling.DoDamage(shipDetails.Primary.Damage); } } else { Ship17LaserDrawCount++; } } laserBeamRenderer.SetPosition(1, endPoint); } }