コード例 #1
0
    void FixedUpdate()
    {
        if (IsDead)
        {
            return;
        }

        float h = Input.GetAxis("Horizontal");

        if (Mathf.Abs(h - 0) > 0.01f)
        {
            Rigidbody2D.velocity = new Vector2(h * MoveSpeed, Rigidbody2D.velocity.y);

            HeroAnim.RunState(h < 0);
        }
        else
        {
            HeroAnim.IdleState();
            Rigidbody2D.velocity = new Vector2(0f, Rigidbody2D.velocity.y);
        }

        if (Rigidbody2D.velocity.y == 0)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Rigidbody2D.AddForce(Vector2.up * JumpPower);
                HeroAnim.JumpStart(h < 0);
            }
            else
            {
                HeroAnim.JumpOver();
            }
        }
    }
コード例 #2
0
    void FixedUpdate()
    {
        if (isDead)
        {
            HeroAnim.DieState();
            return;
        }

        float h = Input.GetAxis("Horizontal");

        if (Mathf.Abs(h - 0) > 0.01f)
        {
            Rigidbody2D.velocity = new Vector2(h * MoveSpeed, Rigidbody2D.velocity.y);

            HeroAnim.WalkState(h < 0);
        }
        else
        {
            HeroAnim.IdleState();
            Rigidbody2D.velocity = new Vector2(0f, Rigidbody2D.velocity.y);
        }

        //detecting the jumping
        if (Rigidbody2D.velocity.y == 0)         //If the hero is not at the ground,he can't jump.
        {
            if (Input.GetKeyDown(KeyCode.Space)) //The space button is pressed.
            {
                Rigidbody2D.AddForce(Vector2.up * JumpPower);
                HeroAnim.JumpState(true);
            }
            else
            {
                HeroAnim.JumpState(false);
            }
        }
    }