コード例 #1
0
    void OnTriggerEnter(Collider other)
    {
        hitVel = rb.velocity;
        hitDir = rb.transform.forward;

        if (other.tag == "Player")
        {
            targetRigidbody     = other.GetComponentInParent <Rigidbody>();
            playerControllerBen = targetRigidbody.GetComponent <PlayerControllerBen>();
            playerHealthAmmo    = targetRigidbody.GetComponent <PlayerHealthAmmo>();

            // If the needle has been thrown and has hit another player
            if (!collectable)
            {
                // Read the targeted player's health and damage/kill it
                if (!playerControllerBen.helmActive)
                {
                    if (playerHealthAmmo.isDead)
                    {
                        tempDeathParticle = Instantiate(playerDeathParticles, other.transform.position, other.transform.rotation) as ParticleSystem;
                        tempDeathParticle.Play();
                        Destroy(tempDeathParticle.gameObject, 2f);
                    }
                    else
                    {
                        playerHealthAmmo.TakeDamage(damage);
                        SoundManager.instance.RandomizeSfx(0.9f, playerHitSounds).Play();
                        // Create hit particles
                        tempCollisionParticle = Instantiate(PlayerHitParticles, other.transform.position, other.transform.rotation) as ParticleSystem;
                        tempCollisionParticle.Play();
                        Destroy(tempCollisionParticle.gameObject, 2f);
                    }
                }
                // Return the needle to the player who threw the needle
                if (shooter != null && shooter.GetComponent <PlayerHealthAmmo>().currentAmmo < 4)
                {
                    shooter.GetComponent <PlayerHealthAmmo>().currentAmmo++;
                }
                // If the needle was generated by a box, give the needle to the player hit
                else if (playerHealthAmmo.currentAmmo < 4)
                {
                    playerHealthAmmo.currentAmmo++;
                }


                gameObject.SetActive(false);
                rb.isKinematic = false;
            }
            // Otherwise let the player pick up the needle
            else if (playerHealthAmmo.currentAmmo < 4)
            {
                playerHealthAmmo.currentAmmo++;
                gameObject.SetActive(false);
                rb.isKinematic   = false;
                transform.parent = null;
            }

            rb.velocity = Vector3.zero;
        }

        if (other.tag == "Box" && transform.parent == null)
        {
            rb.velocity      = Vector3.zero;
            transform.parent = other.transform;

            boxManager = other.GetComponent <BoxManager>();
            boxManager.BoxDamage(boxDamage);

            if (boxManager.isDestroyed)
            {
                other.gameObject.GetComponent <Collider>().enabled = false;

                foreach (Transform child in other.transform)
                {
                    child.gameObject.SetActive(false);
                    child.gameObject.GetComponent <Rigidbody>().isKinematic = false;
                }

                other.transform.DetachChildren();
                gameObject.SetActive(false);
                rb.isKinematic = false;

                // signal if current box has been hit and create explosion
                tempDeathParticle = Instantiate(BoxDeathParticles, other.transform.position, other.transform.rotation) as ParticleSystem;
                tempDeathParticle.Play();
                Destroy(tempDeathParticle.gameObject, 2f);
            }

            else
            {
                SoundManager.instance.RandomizeSfx(0.9f, boxHitSounds).Play();
                tempCollisionParticle = Instantiate(BoxHitParticles, transform.position, transform.rotation) as ParticleSystem;
                tempCollisionParticle.Play();
                Destroy(tempCollisionParticle.gameObject, 2f);
            }
        }

        if (other.tag == "Reflective" && transform.parent == null)
        {
            SoundManager.instance.RandomizeSfx(0.5f, needleRicochets).Play();
            Vector3 deflectDir = Vector3.Reflect(hitDir, other.transform.up);
            deflectDir.y      = 0;
            transform.forward = deflectDir;
            rb.velocity       = transform.forward * hitVel.magnitude;
        }

        if (other.tag == "WoolBall")
        {
            SoundManager.instance.PlaySingle(1f, needleHitWoolBall);
            rb.velocity      = Vector3.zero;
            transform.parent = other.transform;
        }
    }