void OnTriggerEnter2D(Collider2D other) { // If the player enters the trigger zone... if (FakeTag.IsTag(other, "Player")) { // Get a reference to the player health script. PlayerHealth playerHealth = other.GetComponent <PlayerHealth>(); // Increasse the player's health by the health bonus but clamp it at 100. playerHealth.health += healthBonus; playerHealth.health = Mathf.Clamp(playerHealth.health, 0f, 100f); // Update the health bar. playerHealth.UpdateHealthBar(); // Trigger a new delivery. pickupSpawner.StartCoroutine(pickupSpawner.DeliverPickup()); // Play the collection sound. AudioSource.PlayClipAtPoint(collect, transform.position); // Destroy the crate. Destroy(transform.root.gameObject); } // Otherwise if the crate hits the ground... else if (FakeTag.IsTag(other, "ground") && !landed) { // ... set the Land animator trigger parameter. anim.SetTrigger("Land"); transform.parent = null; gameObject.AddComponent <Rigidbody2D>(); landed = true; } }
void OnTriggerEnter2D(Collider2D col) { // If it hits an enemy... if (FakeTag.IsTag(col, "Enemy")) { // ... find the Enemy script and call the Hurt function. col.gameObject.GetComponent <Enemy>().Hurt(); // Call the explosion instantiation. OnExplode(); // Destroy the rocket. Destroy(gameObject); } // Otherwise if it hits a bomb crate... else if (FakeTag.IsTag(col, "BombPickup")) { // ... find the Bomb script and call the Explode function. col.gameObject.GetComponent <Bomb>().Explode(); // Destroy the bomb crate. Destroy(col.transform.root.gameObject); // Destroy the rocket. Destroy(gameObject); } // Otherwise if the player manages to shoot himself... else if (!FakeTag.IsTag(col, "Player")) { // Instantiate the explosion and destroy the rocket. OnExplode(); Destroy(gameObject); } }
void FixedUpdate() { // Create an array of all the colliders in front of the enemy. Collider2D[] frontHits = Physics2D.OverlapPointAll(frontCheck.position, 1); // Check each of the colliders. foreach (Collider2D c in frontHits) { // If any of the colliders is an Obstacle... if (FakeTag.IsTag(c, "Obstacle")) { // ... Flip the enemy and stop checking the other colliders. Flip(); break; } } // Set the enemy's velocity to moveSpeed in the x direction. GetComponent <Rigidbody2D>().velocity = new Vector2(transform.localScale.x * moveSpeed, GetComponent <Rigidbody2D>().velocity.y); // If the enemy has one hit point left and has a damagedEnemy sprite... if (HP == 1 && damagedEnemy != null) { // ... set the sprite renderer's sprite to be the damagedEnemy sprite. ren.sprite = damagedEnemy; } // If the enemy has zero or fewer hit points and isn't dead yet... if (HP <= 0 && !dead) { // ... call the death function. Death(); } }
void OnCollisionEnter2D(Collision2D col) { // If the colliding gameobject is an Enemy... if (FakeTag.IsTag(col.collider, "Enemy")) { // ... and if the time exceeds the time of the last hit plus the time between hits... if (Time.time > lastHitTime + repeatDamagePeriod) { // ... and if the player still has health... if (health > 0f) { // ... take damage and reset the lastHitTime. TakeDamage(col.transform); lastHitTime = Time.time; } // If the player doesn't have health, do some stuff, let him fall into the river to reload the level. else { // Find all of the colliders on the gameobject and set them all to be triggers. Collider2D[] cols = GetComponents <Collider2D>(); foreach (Collider2D c in cols) { c.isTrigger = true; } // Move all sprite parts of the player to the front SpriteRenderer[] spr = GetComponentsInChildren <SpriteRenderer>(); foreach (SpriteRenderer s in spr) { s.sortingLayerName = "UI"; } // ... disable user Player Control script GetComponent <PlayerControl>().enabled = false; // ... disable the Gun script to stop a dead guy shooting a nonexistant bazooka GetComponentInChildren <Gun>().enabled = false; // ... Trigger the 'Die' animation state anim.SetTrigger("Die"); } } } }
public void Explode() { // The player is now free to lay bombs when he has them. layBombs.bombLaid = false; // Make the pickup spawner start to deliver a new pickup. pickupSpawner.StartCoroutine(pickupSpawner.DeliverPickup()); // Find all the colliders on the Enemies layer within the bombRadius. Collider2D[] enemies = Physics2D.OverlapCircleAll(transform.position, bombRadius, 1 << LayerMask.NameToLayer("Enemies")); // For each collider... foreach (Collider2D en in enemies) { // Check if it has a rigidbody (since there is only one per enemy, on the parent). Rigidbody2D rb = en.GetComponent <Rigidbody2D>(); if (rb != null && FakeTag.IsTag(en, "Enemy")) { // Find the Enemy script and set the enemy's health to zero. rb.gameObject.GetComponent <Enemy>().HP = 0; // Find a vector from the bomb to the enemy. Vector3 deltaPos = rb.transform.position - transform.position; // Apply a force in this direction with a magnitude of bombForce. Vector3 force = deltaPos.normalized * bombForce; rb.AddForce(force); } } // Set the explosion effect's position to the bomb's position and play the particle system. explosionFX.transform.position = transform.position; explosionFX.Play(); // Instantiate the explosion prefab. Instantiate(explosion, transform.position, Quaternion.identity); // Play the explosion sound effect. AudioSource.PlayClipAtPoint(boom, transform.position); // Destroy the bomb. Destroy(gameObject); }
void OnTriggerEnter2D(Collider2D col) { // If the player hits the trigger... if (FakeTag.IsTag(col, "Player")) { // .. stop the camera tracking the player GameObject.FindGameObjectWithTag("MainCamera").GetComponent <CameraFollow>().enabled = false; // .. stop the Health Bar following the player if (GameObject.FindGameObjectWithTag("HealthBar").activeSelf) { GameObject.FindGameObjectWithTag("HealthBar").SetActive(false); } // ... instantiate the splash where the player falls in. Instantiate(splash, col.transform.position, transform.rotation); // ... destroy the player. Destroy(col.gameObject); // ... reload the level. StartCoroutine("ReloadGame"); } }
void Update() { // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer. var groundHits = Physics2D.LinecastAll(transform.position, groundCheck.position); grounded = false; if (groundHits != null) { foreach (var hit in groundHits) { if (FakeTag.IsTag(hit.collider, "ground")) { grounded = true; } } } // If the jump button is pressed and the player is grounded then the player should jump. if (Input.GetKeyDown(KeyCode.Space) && grounded) { jump = true; } }
void OnTriggerEnter2D(Collider2D other) { // If the player enters the trigger zone... if (FakeTag.IsTag(other, "Player")) { // ... play the pickup sound effect. AudioSource.PlayClipAtPoint(pickupClip, transform.position); // Increase the number of bombs the player has. other.GetComponent <LayBombs>().bombCount++; // Destroy the crate. Destroy(transform.root.gameObject); } // Otherwise if the crate lands on the ground... else if (FakeTag.IsTag(other, "ground") && !landed) { // ... set the animator trigger parameter Land. anim.SetTrigger("Land"); transform.parent = null; gameObject.AddComponent <Rigidbody2D>(); landed = true; } }