Using GUI buttons to move a sprite (C#)

So basically, I am making a 2 player Brick Breaker game where I have a left and right button for each player.

The problem is that I need the buttons to move a sprite (the paddle) left and right for each player, depending on the button pressed: e.g. if P1Left is pressed, then player 1’s paddle is moved left.

The reason behind this is that I am making this for my computing project and its meant to me an Android App.

The paddle has a Rigidbody2D and BoxCollider added as the componenets.

Any alternative solutions are welcome

Code needs to be in C#

Thanks for your time. effort and help

Create a sprite for left button and another for right… Then attach box collider2d to them. and add two separate scripts to them:

for leftButton:

public GameObject paddle; //attach your paddle Gameobject to this
void OnMouseDown(){
  paddle.GetComponent<Rigidbody>().addVelocity(new Vector3(-1,0,0);
}

And another same script but with change of new(Vector3(1,0,0);

you can adjust speed in new Vector3;

I also used this in my game… You can checkout the button68453-right.png

bool move;
Vector2 newPos;
Rigidbody2D rgbd;

void Start(){
    newPos = new Vector2(0f, 0f);
    rgbd = Gameobject.FindObjectWithTag("paddleP1").GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
    if(move){
        rgbd.movePosition(rgbd.position + newPos);
    }
}

public void leftButton(bool isDown){
    if(isDown){
        move = true;
        newPos.x = -1f;
    }else{
        move = false;
    }
}

public void rightButton(bool isDown){
    if(isDown){
        move = true;
        newPos.x = 1f;
    }else{
        move = false;
    }
}

attach this script to an empty gameObject and name it something like scriptObject, now on your both buttons add EventTrigger component and through this component add new OnPointerDown and OnPointerUp events (keep in mind to do this for both buttons of your both players), these events works same as OnClick means you had to drag and drop your scriptObject on them and then access the script on this gameObject … for left button call leftButton method and for right button call rightButton method just make sure that when calling these methods change the isDown boolean to true in OnPointerDown and false in OnPointerUp … and this example will work if your paddle has a tag named paddleP1 … i think the script is self explanatory in itself