moving player with ui buttons

ive set up my methods to work for a keyboard so for instance this calls my moving left method

if (Input.GetKey(KeyCode.LeftArrow))
    {
        movingLeft();
        animationClips();
    }

i want to call the same method from a ui button i use a bool when true it can move when false it cant and set these to my button in in the pointer up and pointer down to get continual movement something like this

public void MoveMeLeft()
{
    moveLeft = true;
}

public void StopMeLeft()
{
    moveLeft = false;
}

and then check them and move accordingly like this

     if (moveLeft && !moveRight)
    {
        movingLeft();
        animationClips();
    }

    if (moveRight && !moveLeft)
    {
        movingRight();
        animationClips();
    }

but this doesnt seem to work, if i add a debug.log(“player should be moving left”) in my moving left method it prints to the screen but no movement happens despite it being the same method im calling for the keyboard and it works fine, can any body tell me whats going on here?

heres my movingLeft method,

public void movingLeft()
{
    moveHorizontal = Input.GetAxis("Horizontal");
    Debug.Log("i should be moving left"); //this displays in the console when pressing keyboard or pressing the ui button
    Vector3 movement = new Vector3(moveHorizontal, 0, 0);
    playerRigidbody.AddForce(movement * speed);
    if (moveHorizontal < 0)
    {
        jumpDirection = false;
        moveDirection = false;
    }
}

any and all suggestions welcome, many thanks

Try changing your function like this. Change Speed Secs describes, starting at the moment you start pressing the left button, how much time it will take to change movement.x from 0 to -1 (in the GetAxis version, you’re using analog values so change speed is less relevant)

Vector2 movement = Vector2.zero;

public void movingLeft(float moveInput, float changeSpeedSecs)
 {
     movement.x = Mathf.max(-1, movement.x - moveInput * (Time.deltaTime / changeSpeedSecs));
     playerRigidbody.AddForce(movement * speed);
     if (moveInput < 0)
     {
         jumpDirection = false;
         moveDirection = false;
     }
 }

//Example usage for GetAxis, with fast change time since InputManager applies this to axes already:
movingLeft(Input.GetAxis("Horizontal"), 0.01f);

//Example usage for buttons, using slower change time to prevent abrupt start/stop
movingLeft(-1, 0.5f);

I’m not totally sure how your code is structured (why moveLeft/Right instead of a single moveHorizontal method) but this example should work for you. To make the “movingRight” version, you’d just have to flip a couple negatives to positives.