Esempio n. 1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        //Check if character just landed on the ground
        if (!m_grounded && m_groundSensor.State())
        {
            m_grounded = true;
        }

        //Check if character just started falling
        if (m_grounded && !m_groundSensor.State())
        {
            m_grounded = false;
        }

        // -- Handle input and movement --
        float inputX = Input.GetAxis("Horizontal");

        // Swap direction of sprite depending on walk direction
        if (inputX < 0)
        {
            GetComponent <SpriteRenderer>().flipX = true;
        }
        else if (inputX > 0)
        {
            GetComponent <SpriteRenderer>().flipX = false;
        }
    }
    private void Update()
    {
        if (OnStateChange == null)
        {
            return;
        }

        if (Input.GetAxis("Horizontal") == 0 || (groundSensor.State()))
        {
            onGround = true;
            PlayerFSM.Instance.ChangeState(new Idle());
        }

        if (Input.GetAxis("Horizontal") != 0)
        {
            PlayerFSM.Instance.ChangeState(new WalkState(Input.GetAxis("Horizontal")));
        }

        if (Input.GetAxis("Vertical") > 0 && onGround)
        {
            onGround = false;
            groundSensor.Disable(0.2f);
            PlayerFSM.Instance.ChangeState(new Jump());
        }

        if (PlayerFSM.Instance.ValidUpdate)
        {
            OnStateChange.Invoke(PlayerFSM.Instance.CurrentState);
            PlayerFSM.Instance.Reset();
        }
    }