コード例 #1
0
    void Update()
    {
        var fireButton1 = Input.GetAxisRaw("Fire1") != 0 || Input.GetKeyDown(KeyCode.Space);
        var fireButton2 = Input.GetAxisRaw("Fire2") != 0;

        //If Fire1 and not attacking, the player can attack
        if (fireButton1)
        {
            if (isAttackAxisAlreadyDown == false) // gate to only take attack input the frame the butotn is down
            {
                isAttackAxisAlreadyDown = true;
                if (canAttack)
                {
                    stateMachine.RequestChangePlayerState(CharacterStateMachine.CharacterState.attacking);
                    StartCoroutine(ReturnToIdleWhenAttackIsOver());
                }
            }
        }

        if (!fireButton1) // reset attack input gate
        {
            isAttackAxisAlreadyDown = false;
        }

        //Ff Fire2 and there are dashing points available, the player will dash
        if (fireButton2)
        {
            if (isDashAxisAlreadyDown == false) // gate to only take dash input the frame the button is down
            {
                isDashAxisAlreadyDown = true;
                if (canDash)
                {
                    stateMachine.RequestChangePlayerState(CharacterStateMachine.CharacterState.dashing);
                    PerformDashing();
                }
            }
        }

        if (!fireButton2) // reset dash input gate
        {
            isDashAxisAlreadyDown = false;
        }

        //If can move and its not attacking, change the state to walking
        if (canMove)
        {
            MovePlayer();

            if (stateMachine.GetCurrentState() != CharacterStateMachine.CharacterState.dashing)
            {
                if (lastPosition != gameObject.transform.position)
                {
                    stateMachine.RequestChangePlayerState(stateModifier: CharacterStateMachine.CharacterState.walking);
                }
                else
                {
                    stateMachine.RequestChangePlayerState(CharacterStateMachine.CharacterState.idle);
                }
            }
        }

        lastPosition = gameObject.transform.position;
    }