コード例 #1
0
        void OnTriggerEnter2D(Collider2D other)
        {
            // If the player enters the trigger zone...
            if (other.tag == "Player")
            {
                // Get a reference to the player health script.
                PlayersHealth playerHealth = other.GetComponent <PlayersHealth> ();

                // 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 (other.tag == "ground" && !landed)
            {
                // ... set the Land animator trigger parameter.
                anim.SetTrigger("Land");

                transform.parent = null;
                gameObject.AddComponent <Rigidbody2D> ();
                landed = true;
            }
        }
コード例 #2
0
ファイル: Bomb.cs プロジェクト: bryanrtboy/wireframe-workshop
        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.rigidbody2D;
                if (rb != null && rb.tag == "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);
        }