Beispiel #1
0
    // Checks aim input based on last control type
    private void HandleWeaponAimInput()
    {
        // If Weapon Disabled then stop aiming and return
        if (bWeaponDisabled)
        {
            CurrentWeapon.SetWeaponDisabled(true);
        }
        else
        {
            CurrentWeapon.SetWeaponDisabled(false);
        }

        if (motor.motorState == PlatformerMotor2D.MotorState.Dashing ||
            motor.motorState == PlatformerMotor2D.MotorState.ClimbingCorner)
        {
            return;
        }

        // If aiming
        if (bAimState)
        {
            // Last used control type
            Controller lastController = playerInput.controllers.GetLastActiveController();

            // Aim Position Difference (From Aim Pivot)
            Vector2 diff = Vector2.zero;
            // Aim IK Offset Distance (Used to calculate aim difference from aim pivot to weapon fire point)
            Vector2 ikOffset = Vector2.zero;

            // If using Keyboard and Mouse then aim with mouse position
            if (lastController.type == ControllerType.Keyboard ||
                lastController.type == ControllerType.Mouse)
            {
                diff     = animator.weaponPivotIKBone.GetWorldPosition(animator.transform) - gameCamera.ScreenToWorldPoint(Input.mousePosition);
                ikOffset = new Vector2(diff.y, -diff.x).normalized *CurrentWeapon.IKOffset;

                if (diff.x >= 0)
                {
                    ikOffset *= -1;
                }

                diff += ikOffset;

                cameraPointer.bGamepadAim = false;
            }
            else if (lastController.type == ControllerType.Joystick)             // Otherwise, use aim input axes from gamepad
            {
                diff = -playerInput.GetAxis2D("Aim Horizontal", "Aim Vertical").normalized;

                // If aim input is zero
                if (diff == Vector2.zero)
                {
                    // If previously was aiming then maintain aim
                    if (v2PrevAimDiff != Vector2.zero)
                    {
                        diff = v2PrevAimDiff;
                    }
                    else                     // Otherwise, aim forward based on facing direction
                    {
                        diff = bFacingLeft ? Vector2.right : Vector2.left;

                        // If on corner and arm is not busy, reverse aim to flip character to face corner again
                        if (bOnCorner && !animator.ArmBusy)
                        {
                            diff.x *= -1;
                        }
                    }
                }

                cameraPointer.bGamepadAim = true;
            }

            v2PrevAimDiff = diff;

            // If direction is not locked then set facing based on direction
            if (!bLockedDirection)
            {
                if (diff.x >= 0)
                {
                    bFacingLeft = true;
                    diff.x     *= -1;

                    if (!cameraPointer.bGamepadAim)
                    {
                        diff.x += 0.035f;
                    }
                }
                else
                {
                    bFacingLeft = false;
                }
            }
            else
            {
                if (bFacingLeft)
                {
                    diff.x *= -1;

                    if (!cameraPointer.bGamepadAim)
                    {
                        diff.x += 0.035f;
                    }
                }
            }

            CalculateWeaponAim(diff);
            CalculateHeadAim(diff);

            bAimingDownSights = true;
        }
        else         // Otherwise
        {
            // If was previously aiming
            if (bPrevAimState)
            {
                // If direction is not locked then set motor facing based on previous aim direction
                if (!bLockedDirection)
                {
                    if (v2PrevAimDiff.x >= 0)
                    {
                        bFacingLeft = true;
                    }
                    else
                    {
                        bFacingLeft = false;
                    }
                }

                motor.facingLeft = bFacingLeft;
            }
            else             // Otherwise
            {
                // If not reloading then reset aiming
                if (!CurrentWeapon.Reloading && !bHolsteringWeapon && !bUnholsteringWeapon)
                {
                    v2PrevAimDiff = Vector2.zero;
                }

                // Ready stance aiming
                CalculateWeaponAim(-Vector2.right);
            }
        }

        bPrevAimState = bAimState;

        // If switching weapons, return
        if (bHolsteringWeapon || bUnholsteringWeapon || !bHoldingWeapon)
        {
            animator.SetArmBusy(false);
            bAimingDownSights = false;

            if (playerInput.GetButton("Aim Mode"))
            {
                bAimState = true;
            }
        }
    }