void OnTriggerEnter2D(Collider2D collider) { ObjectTilingCtrl tile = collider.GetComponent <ObjectTilingCtrl> (); if (collider.tag == "Asteroid" && tile == null) { // This is an untiled asteroid, so tile it tilingCtrl.TileAnObject(collider.gameObject); } }
// If we get hit, do we die, or shrink down? void DownSize(Vector3 normalHit) { // Play sound GameObject audioPlayerObject = Instantiate(settings.audioPlayerPrefab, transform.position, Quaternion.identity) as GameObject; AudioPlayer aPlayer = audioPlayerObject.GetComponent <AudioPlayer> (); if (aPlayer) { aPlayer.aSource.clip = settings.asteroidShrink; } if (size <= 1) { scoreCtrl.AddPoints(settings.pointsPerSmallDestroyed); Kill(); Destroy(gameObject); } else { if (size == 2) { scoreCtrl.AddPoints(settings.pointsPerMediumDestroyed); } else if (size == 3) { scoreCtrl.AddPoints(settings.pointsPerSmallDestroyed); } size--; GameObject smallerAsteroid = size == 1 ? settings.smallAsteroid : size == 2 ? settings.mediumAsteroid : settings.largeAsteroid; GameObject spawned; float offset = smallerAsteroid.GetComponent <Collider2D> ().bounds.size.magnitude; for (int i = 0; i < settings.smallerAsteroidsSpawnedOnKill; i++) { Quaternion rot = Quaternion.FromToRotation(Vector3.up, normalHit); Vector3 direction = rot * normalHit; //Quaternion.AngleAxis( i == 0 ? 90f : -90f, normalHit) * normalHit; spawned = Instantiate(smallerAsteroid, transform.position + (2 * offset * (i == 0 ? direction : -direction)), Quaternion.identity) as GameObject; tilingCtrl.TileAnObject(spawned); spawned.GetComponent <Rigidbody2D> ().AddForce(settings.asteroidSpawnForce * (i == 0 ? direction : -direction)); } Kill(); // Shouldn't be necessary, this object should be destroyed in the Kill() function before it gets here. // Just for my own sanity Destroy(gameObject); } }
// Calculate whether we're on the screen completely (and hence tile and fix layers/tags) // or just a bit - enable the normal collision process void FixedUpdate() { Vector3 bottomLeft = cam.ScreenToWorldPoint(new Vector3(0f, 0f, cam.nearClipPlane)); Vector3 topRight = cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane)); Bounds bounds = coll.bounds; Vector3 myBottomLeft = bounds.min; Vector3 myTopRight = bounds.max; if (myTopRight.x < topRight.x && myTopRight.y < topRight.y && myBottomLeft.x > bottomLeft.x && myBottomLeft.y > bottomLeft.y) { if (tiling) { tiling.TileAnObject(gameObject); } // if only partially in the screen } else if (myTopRight.x < topRight.x || myTopRight.y < topRight.y || myBottomLeft.x > bottomLeft.x || myBottomLeft.y > bottomLeft.y) { coll.isTrigger = false; } }