Esempio n. 1
0
    private void OnTriggerEnter(Collider otherCollider)
    {
        if (otherCollider.gameObject.GetComponent <Barrel>() != null)
        {
            //Collect ammo crates.
            Barrel barrel = otherCollider.gameObject.GetComponent <Barrel>();
            if (barrel.type == "vanille")
            {
                vanilleCount = initialScoops;
                barrel.Pickup();
                ObjectPoolingManager.Instance.vanilleBarrelActive = false;
            }
            else if (barrel.type == "schokolade")
            {
                schokoladeCount = initialScoops;
                barrel.Pickup();
                ObjectPoolingManager.Instance.schokoladeBarrelActive = false;
            }
        }

        if (isHurt == false)
        {
            GameObject hazard = null;
            if (otherCollider.gameObject.GetComponent <Enemy>() != null)
            {
                //Touching Enemys
                Enemy enemy = otherCollider.gameObject.GetComponent <Enemy>();
                hazard  = enemy.gameObject;
                health -= enemy.damage;
            }
            else if (otherCollider.GetComponent <Bullet>() != null)
            {
                Bullet bullet = otherCollider.GetComponent <Bullet>();
                if (bullet.ShotByPlayer == false)
                {
                    hazard  = bullet.gameObject;
                    health -= bullet.damage;
                    HeartController.Instance.ReduceHeart();
                    m_AudioSource.clip = m_playerHurt;
                    m_AudioSource.Play();
                    bullet.gameObject.SetActive(false);
                }
            }

            if (hazard != null)
            {
                isHurt = true;

                //Perform the knockback effect
                Vector3 hurtDirection      = (transform.position - hazard.transform.position).normalized;
                Vector3 knockbackDirection = (hurtDirection + Vector3.up).normalized;
                GetComponent <ForceReciever>().AddForce(knockbackDirection, knockbackForce);

                StartCoroutine(HurtRoutine());
            }

            if (health <= 0)
            {
                if (killed == false)
                {
                    killed = true;
                    OnKill();
                }
            }
        }
    }