Exemplo n.º 1
0
    private void HandleHorizontalMovement()
    {
        float h = Input.GetAxis("Horizontal");

        //sudden stop
        if (h == 0)
        {
            if (IsRunning())
            {
                StopRunning();
            }
        }
        else
        {
            soundFx.StartRunSound();
        }

        // The Speed animator parameter is set to the absolute value of the horizontal input.
        anim.SetFloat("Speed", Mathf.Abs(rb2D.velocity.x));

        // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
        if (h * rb2D.velocity.x < maxSpeed)
        {
            rb2D.AddForce(Vector2.right * h * moveForce);
        }

        if (Mathf.Abs(rb2D.velocity.x) > maxSpeed)
        {
            rb2D.velocity = new Vector2(Mathf.Sign(rb2D.velocity.x) * maxSpeed, rb2D.velocity.y);
        }

        if (h > 0 && !facingRight)
        {
            Flip();
        }
        else if (h < 0 && facingRight)
        {
            Flip();
        }
    }