コード例 #1
0
    private void FireBullet(Vector2 shootRef)
    {
        //Declare and Define Starting Shoot Position
        Vector2 startingShootPos;

        if (shootLeft)
        {
            startingShootPos = leftFireRef.transform.position;
        }

        else
        {
            startingShootPos = rightFireRef.transform.position;
        }

        //Pull bullet Reference from Pooler
        bullet = ObjectPooler.sharedInstance.GetPooledObject("playerBullet");

        if (bullet != null) //Peform Null-Check on bullet
        {
            //Set bullet position & rotation to that of player character
            bullet.transform.position = startingShootPos;
            bullet.transform.rotation = gameObject.transform.rotation;
            bullet.SetActive(true);                                                                              //Spawn Bullet
            Physics2D.IgnoreCollision(bullet.GetComponent <CircleCollider2D>(), GetComponent <BoxCollider2D>()); //Create collision exception for bullet col & player col

            //Pull Reference to Neccesary bullet Components
            Rigidbody2D  bulletSpawnedRB    = bullet.GetComponent <Rigidbody2D>();
            PlayerBullet bulletPlayerBullet = bullet.GetComponent <PlayerBullet>();

            //Add Calculate Updated 'shoot' Vector2
            Vector2 uniformShootPos = CalculateShootPos(shootRef); //Converts pure stick data into uniform Vector2 (ensures bullets fire at same speed, regardless of right stick distance from perimeter)

            //Check Player Velocity Against Threshold to Speed up Bullets - Prevents player from catching up to bullets
            if (playerRB.velocity.magnitude > playerSpeedAccelerateShootThreshold)
            {
                Vector2 magnitudeMultipliedShootPos = uniformShootPos.normalized * (playerRB.velocity.magnitude / playerSpeedAccelerateShootThreshold); //Apply magnitude multiplier to normalised shoot position
                Vector2 finalShootPos = magnitudeMultipliedShootPos * bulletPlayerBullet.GetBulletSpeed();                                              //Create final shoot position by multiplying by bullet speed
                bulletSpawnedRB.AddForce(finalShootPos);                                                                                                //Update bullet rigidbody
            }

            //If Player Velocity Threshold not met, Fire Bullet Normally
            else
            {
                bulletSpawnedRB.AddForce(new Vector2((uniformShootPos.x * bulletPlayerBullet.GetBulletSpeed()), (uniformShootPos.y * bulletPlayerBullet.GetBulletSpeed()))); //Update bullet rigidbody
            }
        }

        shootLeft = !shootLeft; //Invert value of shootLeft
    }