How to make a UI button emulate a keyboard key

i made a scrip but it is for a keyboard:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    //Movement
    public float speed;
    public float jump;
    float moveVelocity;

    //Grounded Vars
    bool grounded = true;

    void Update()
    {
        //Jumping
        if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
        {
            if (grounded)
            {
                GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
            }
        }

        moveVelocity = 0;

        //Left Right Movement
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            moveVelocity = -speed;
        }
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            moveVelocity = speed;
        }

        GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);

    }
    //Check if Grounded
    void OnTriggerEnter2D()
    {
        grounded = true;
    }
    void OnTriggerExit2D()
    {
        grounded = false;
    }
}

but now i need to make 3 buttons one to jump ( emulates the arrow up key) ,
one to move right ( right arrow key ) and one to mave left ( left arrow key )

There are many, many problems with your code.

  1. A rigidbody must be moved in the FixedUpdate function, not the Update one. Indeed, all the operation involving Physics should be handled in the FixedUpdate function

  2. Cache your Rigidbody component!!!

  3. No, no, no, and no! UI buttons should not “emulate” key presses, simply call the same functions using KeyPresses and UI, for this, just create specific functions in your PlayerController

  4. Use the InputManager to handle your controls Unity - Manual: Input Manager

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    private const jumpAxisName = “Jump”;
    private const verticalAxisName = “Vertical”;
    private const horizontalAxisName = “Horizontal”;

    //Movement
    public float speed;
    public float jump;
    private Vector2 velocity ;
    
    //Grounded Vars
    bool grounded = true;
    
    private Rigidbody2D rigidbody2D ;
    
    void Awake()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }
    
    void Update()
    {
        float horizontal = Input.GetAxis( horizontalAxisName ) ;
    
        if( horizontal > Mathf.Epsilon )
            MoveRight() ;
        else if( horizontal < -Mathf.Epsilon )
            MoveLeft() ;
    
        //Jumping
        if ( Input.GetAxis(jumpAxisName) > Mathf.Epsilon || Input.GetAxis(verticalAxisName) > Mathf.Epsilon )
        {
            Jump() ;
        }
    }
    
    void FixedUpdate()
    {   
        rigidbody2D.AddForce(velocity, ForceMode.VelocityChange);
        velocity.y = 0 ;
    }    
    
    //Check if Grounded
    void OnTriggerEnter2D()
    {
        grounded = true;
    }
    
    void OnTriggerExit2D()
    {
        grounded = false;
    }
    
    // Call this function using your UI buttons
    public void Jump()
    {
        if (grounded)
        {
            velocity.y = jump ;
        }
    }
    
    // Call this function from the `OnPointerDown` event of the `EvenTrigger`
    // component you attach to your right button
    public void MoveRight()
    {
        velocity.x = speed;
    }
    
    // Call this function from the `OnPointerDown` event of the `EvenTrigger`
    // component you attach to your left button
    public void MoveLeft()
    {
        velocity.x = -speed;
    }
    
    // Call this function from the `OnPointerUp` event of the `EvenTrigger`
    // component you attach to your buttons
    public void StopMoving()
    {
        velocity.x = 0;
    }
    

    }