void BulletRay(Vector2 origin, Vector2 direction)
    {
        RaycastHit2D hitInfo;

        hitInfo = Physics2D.Raycast(origin, direction, -0.5f, WhatToHit);

        if (hitInfo.collider != null)
        {
            // Release bullet trail from gameobject
            if (transform.childCount > 0)
            {
                GameObject bulletTrail = transform.GetChild(0).gameObject;
                bulletTrail.transform.parent     = null;
                bulletTrail.transform.localScale = new Vector3(0.05f, 0.05f, 1f);
            }

            if (hitInfo.transform.gameObject.tag == "Foreground" || hitInfo.transform.gameObject.tag.Equals("Door"))
            {
                // First instantiate collision effect then destroy the bullet
                var collEffectGO = Instantiate(CollisionEffect, transform.position, Quaternion.identity);
                if (ScaleXValue < 0)
                {
                    collEffectGO.transform.localScale = new Vector2(-1f, 1f);
                }

                Destroy(gameObject);
            }

            if (hitInfo.transform.gameObject.tag == "Coverable Object")
            {
                float additionalCollEffectPosX = -0.2f;
                if (ScaleXValue < 0)
                {
                    additionalCollEffectPosX = Mathf.Abs(additionalCollEffectPosX);
                }

                Vector2 collEffectPos = new Vector2(transform.position.x + additionalCollEffectPosX, transform.position.y);

                var collEffectGO = Instantiate(CollisionEffect02, collEffectPos, Quaternion.identity);
                if (ScaleXValue < 0)
                {
                    collEffectGO.transform.localScale = new Vector2(-1f, 1f);
                }

                Instantiate(CoverableCollisionEffect01, transform.position, Quaternion.identity);

                CoverPerformableObjectBehaviour coverObj = hitInfo.transform.parent.GetComponent <CoverPerformableObjectBehaviour>();

                if (coverObj != null)
                {
                    coverObj.GetDamage(15f);
                }

                Destroy(gameObject);
            }

            if (hitInfo.transform.gameObject.tag == "Bottle")
            {
                // Break bottle
                BottleBehaviour bottle = hitInfo.transform.GetComponent <BottleBehaviour>();

                if (bottle != null && !_hasBottleBroken)
                {
                    bool result = bottle.BreakBottle();
                    _hasBottleBroken = result;
                }
            }

            PlayerController player = hitInfo.transform.GetComponent <PlayerController>();

            if (player != null)
            {
                // Check if bullet is colliding with umbrella
                if (player.IsPlayerCoveredByUmbrella() && !ScaleXValue.Equals(player.transform.localScale.x))
                {
                    Instantiate(ElectricityCollisionEffect, transform.position, Quaternion.identity);
                    Destroy(gameObject);
                    return;
                }

                Destroy(gameObject);
                // If bullet collided with player

                player.GetDamage(1f, WeaponType, 0);
            }
        }
    }
コード例 #2
0
    void BulletRay(Vector2 origin, Vector2 direction)
    {
        RaycastHit2D hitInfo;

        hitInfo = Physics2D.Raycast(origin, direction, -0.5f, WhatToHit);

        if (hitInfo.collider != null)
        {
            // Release bullet trail from gameobject
            if (transform.childCount > 0)
            {
                GameObject bulletTrail = transform.GetChild(0).gameObject;
                bulletTrail.transform.parent     = null;
                bulletTrail.transform.localScale = new Vector3(0.05f, 0.05f, 1f);
            }

            if (hitInfo.transform.gameObject.tag.Equals("Foreground") || hitInfo.transform.gameObject.tag.Equals("Door"))
            {
                //First instantiate collision effect then destroy the bullet.

                float additionalCollEffectPosX = -0.2f;
                if (_scaleXValue < 0)
                {
                    additionalCollEffectPosX = Mathf.Abs(additionalCollEffectPosX);
                }

                Vector2 collEffectPos = new Vector2(transform.position.x + additionalCollEffectPosX, transform.position.y);

                var collEffectGO = Instantiate(CollisionEffect, collEffectPos, Quaternion.identity);
                if (_scaleXValue < 0)
                {
                    collEffectGO.transform.localScale = new Vector2(-1f, 1f);
                }

                float additionalCollEffect2PosX = -0.1f;

                if (_scaleXValue < 0)
                {
                    additionalCollEffect2PosX = Mathf.Abs(additionalCollEffect2PosX);
                }

                Destroy(gameObject);
            }

            if (hitInfo.transform.gameObject.tag.Equals("Bottle"))
            {
                // Break bottle
                BottleBehaviour bottle = hitInfo.transform.GetComponent <BottleBehaviour>();

                if (bottle != null && !_hasBottleBroken)
                {
                    bool result = bottle.BreakBottle();
                    _hasBottleBroken = result;
                }
            }

            EnemyController enemy = hitInfo.transform.GetComponent <EnemyController>();

            if (enemy != null)
            {
                Destroy(gameObject);
                enemy.GetDamage(_scaleXValue, WeaponType, 0);
            }
        }
    }