Пример #1
0
    void OnTriggerEnter2D(Collider2D otherCollider)                       //Activates for collision with a Trigger Object
    {
        pWeapon shot = otherCollider.gameObject.GetComponent <pWeapon>(); // Creates shot variable that grabs elements from the Projectile Script

        if (shot != null)                                                 //Check for shot to be a real game object and not a ghost object
        {
            if (shot.isEnemyWeapon != isEnemy)                            //Checks for friendly fire. Only applies when enemies
            {
                Damage(shot.damage);
                if (shot.weapon)
                {
                    Destroy(shot.gameObject); //Destroys the projectile on impact
                }
            }
        }
    }
    //--------------------------------
    // 3 - Shooting from another script
    //--------------------------------

    public void Attack(bool isEnemy)
    {     //Creates a projectile with its components
        if (CanAttack)
        { //Cooldown has reached zero
            procCooldown = procRate;

            // Create a new projectile
            var weaponTransform = Instantiate(weaponPrefab) as Transform;

            // Assign position
            weaponTransform.position = transform.position;

            // The is enemy property
            pWeapon proc = weaponTransform.gameObject.GetComponent <pWeapon>();
            if (proc != null)
            {
                proc.isEnemyWeapon = isEnemy;
            }

            // Make the weapon shot always towards target
            ProjMovement move = weaponTransform.gameObject.GetComponent <ProjMovement>();
            if (isEnemy)
            {
                if ((eTarget.transform.position.x - transform.position.x) < 0)
                {
                    move.direction = -this.transform.right;
                }
                else
                {
                    move.direction = this.transform.right;
                }
            }
            else
            {
                if (move != null)
                {
                    move.direction = this.transform.right; // towards in 2D space is the right of the sprite (CHANGE THIS TO THE DIRECTION THE PLAYER FACES)
                }
            }
        }
    }