Touch control for a Pong Game

Hey Guys,
can you please help me with changing keybord input script to touch control?

Here is the original script (Keyboard input):

    public float paddleSpeed = 1f;
	public float xLeft;
	public float xRight;
    private Vector3 playerPos = new Vector3(0, -9.5f, 0);

	void Update () {
		float xPos = transform.position.x + (Input.GetAxis("Horizontal") * paddleSpeed);
		playerPos = new Vector3 (Mathf.Clamp (xPos, xLeft, xRight), -9.5f, 0f);
		transform.position = playerPos;
	}

For now, I have this for touch control:

public float paddleSpeed = 1f;
public float xLeft;
public float xRight;

private Ray ray;
private RaycastHit rayCastHit;

private Vector3 playerPos = new Vector3(0, -9.5f, 0);

void Update () {

		if (Input.GetMouseButton (0))
		{
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);

			if(Physics.Raycast(ray, out rayCastHit)){
          //Missing code, not sure how to implement Mathf.Clamp and Vector3 with Ray
   }
}

Missed the tag! :¬)

OK well I don’t use 2D games myself but you can try this if you want to use ray casting:

public void MyRaycast()
	{
		myRay = Camera.main.ScreenPointToRay(Input.mousePosition);

		if(Physics.Raycast(myRay, out rayHitOut))
		{
			Debug.Log ("x = " + rayHitOut.point.x + "  y = " + rayHitOut.point.y);
			Debug.Log (Input.mousePosition);
            Debug.Log (Camera.main.ScreenToWorldPoint(Input.mousePosition));
		}
	}

That just outputs the various Debug info, you only really need the x value. As I don’t use 2D I don’t know what your results are likely to be but note the x value of the raycast and the x value of the mouse outputs and which, if any match up to what you expect from that tutorial.

Then we’ll set the Clamp based on your results. You might want to consider the speed of the platform as well, with your method the paddle can go from one side of the screen to the other pretty fast, you might want 2 buttons like I said in the first comment we can make them act like a controller that gains value/speed if you hold it down.