My 2D character keeps moving with buttons

Hello,

I am having problems when configuring my character movement. The thing is, I am trying to do it with on screen buttons for mobile devices. When I use the button tu jump it works good, but the buttons for horizontal movement make my character move non stop. I am very new with Unity so I really have no clue what can be the problem. I will appreciate any help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMoveJoystick : MonoBehaviour
{
    Rigidbody2D rb2D;

    public float runSpeed = 1.25f;

    public float jumpSpeed = 3;

    public float runSpeedHorizontal = 2;

    public SpriteRenderer spriteRenderer;

    public Animator animator;

    private void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (rb2D.velocity.x > 0)
        {
            spriteRenderer.flipX = false;
            animator.SetBool("Run", true);

        }
        else if (rb2D.velocity.x < 0)
            {
            spriteRenderer.flipX = true;
            animator.SetBool("Run", true);
        }

        else
        {
            animator.SetBool("Run", false);
        }

        if (Input.GetKey("space") && CheckGround.isGrounded)
        {
            rb2D.velocity = new Vector2(rb2D.velocity.x, jumpSpeed);
        }

        if (CheckGround.isGrounded == false)
        {
            animator.SetBool("Jump", true);
            animator.SetBool("Run", false);
        }

        if (CheckGround.isGrounded == true)
        {
            animator.SetBool("Jump", false);
            animator.SetBool("Falling", false);
        }

        if (rb2D.velocity.y < 0)
        {
            animator.SetBool("Falling", true);
        }

        else if (rb2D.velocity.y > 0)
        {
            animator.SetBool("Falling", false);
        }
    }

    public void buttonJump()
    {
        if (CheckGround.isGrounded)
        {
            rb2D.velocity = new Vector2 (rb2D.velocity.x, jumpSpeed);
        }
    }

    public void buttonRight()
    {

        rb2D.velocity = new Vector2 (runSpeed, rb2D.velocity.y);
        spriteRenderer.flipX = false;
        animator.SetBool("Run", true);

    }

    public void buttonLeft()
    {

        rb2D.velocity = new Vector2 (-runSpeed, rb2D.velocity.y);
        spriteRenderer.flipX = true;
        animator.SetBool("Run", true);

    }
}

The jump works because in the Update function you have these lines of code:

  • if (Input.GetKey(“space”) && CheckGround.isGrounded)
  • {
  • rb2D.velocity = new Vector2(rb2D.velocity.x, jumpSpeed);
  • }

With this you don’t need the method “buttonJump()” that does the same thing.

For horizontal movement instead, you only have declared the two methods (buttonRight and buttonLeft) but you haven’t called them in the Update function yet.

So you just need to call them like this:

  • private void Update()
  • {
  • buttonRight();
  • buttonLeft();
  • }