Beispiel #1
0
 private bool TryWeapon(WeaponCodes weaponCode){
     if(ps.HaveAmmo((int)weaponCode)){//Check if we have Ammo of given weapon
         //Equip weapon
         curWeapon = (int)weaponCode;
         weaponContr.ChangeWeapon((int)weaponCode);
         return true;
     }
     return false;
 }
Beispiel #2
0
    private bool CalculateParabolicShotAngles(Transform _target, WeaponCodes weaponCode){
        //Source position
        float x1 = weaponContr.weaponPivot.position.x;
        float y1 = weaponContr.weaponPivot.position.y;

        //Target position
        float x = _target.position.x;
        float y = _target.position.y;

        //Target position normalized by moving source to (0,0)
        x = x - x1;
        y = y - y1;

        //Grab Velocity for specific projectile
        float v = velocity[(int)weaponCode];

        //Get gravity from Unity Project Settings (assuming there is only gravity on the y axis)
        float g = -Physics2D.gravity.y;

        //Check if the target is reachable
        //discriminant = v^4 - g(gx^2 + 2yv^2)
        float discriminant = v*v*v*v - g * (g*x*x + 2*y*v*v);

        if(discriminant < 0){
            //In this case the target is unreachable 
            return false;
        }
        //Theta = ( v^2 (+-) sqrt(discriminant) ) / ( gx )
        angle_1 =  Mathf.Atan2((v * v - Mathf.Sqrt(discriminant)) , (g * x)) * Mathf.Rad2Deg; //Low trajectory
        angle_2 =  Mathf.Atan2((v * v + Mathf.Sqrt(discriminant)) , (g * x)) * Mathf.Rad2Deg; //High trajectory
        /*
        if(x < 0){
            if(y < 0){//3rd quadrant
                //1st to 3rd quadrant
                angle_1 -= 180f;
                angle_2 -= 180f;
            }else{//2nd quadrant
                //4th to 2nd quadrant
                angle_1 += 180f;
                angle_2 += 180f;
            }
        }
        */
        return true;
    }