Moving character with touch buttons (Android)

hello folks,

I wanted to start my first smartphone project with touch responsibility.
I added a character with a rigibody2D attached to it and wrote a script, attached to the charater, too.
Now my problem is he doesnt react and nothing is triggered at all.
If you can show me my problem or how I can do it with two UI buttons to move left or right I would really Appreciate it.

My Code

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour {

	float forwardSpeed = 10f;
	private float w;

	// Use this for initialization
	void Start () {
		w = Screen.width;
	}
	
	// Update is called once per frame
	void Update () {
		int i = 0;
		
		// Loop over every touch found
		while (i < Input.touchCount)
		{
			// Is this the beginning phase of the touch?
			if (Input.GetTouch(i).phase == TouchPhase.Began)
			{
				Debug.Log ("Triggered: " + Input.GetTouch(i).position.x);
				// Does the touch happens on the right side of the screen?
				if (Input.GetTouch(i).position.x > w / 2) {
					// Move your character right
					Debug.Log ("Triggered: " + Input.GetTouch(i).position.x);
					rigidbody2D.AddForce (Vector2.right * forwardSpeed);
					
				}
				
				// Does the touch happens on the left side of the screen?
				if (Input.GetTouch(i).position.x < w / 2){
					Debug.Log ("Triggered: " + Input.GetTouch(i).position.x);
					// Move your character left
					rigidbody2D.AddForce (Vector3.left * forwardSpeed);
					
				}
			}
			++i;
	}
}
}

OK I reformatted your code be careful with that as it makes it harder to read and probably one of the reasons you were in moderation for so long.

This is easy with the new UI, put a canvas with a left and right button on it.

Add Component → Event Trigger for both and add both a PointerEnter and PointerExit (or PointerDown/PointerUp) should be the same for touch devices.

Now create a script that has 2 public functions that can be accessed by you pointer events. That script can either be move once or (and this is why we also add PointerExit/PointerUp) set a bool to true so it moves all the time if that bool is true:

using UnityEngine.UI;   // add to the top

private bool moveLeft;
private bool moveRight;

void Update()
{
    if(moveLeft && !moveRight)
        rigidbody2D.AddForce (Vector3.left * forwardSpeed);

    if(moveRight && !moveLeft)
        rigidbody2D.AddForce (Vector2.right * forwardSpeed);
}

public void MoveMeLeft()
{
    moveLeft = true;
}

public void StopMeLeft()
{
    moveLeft = false;
}

// do the same for right

Drag that script onto say the canvas and in the event trigger(s) click + and a slot appears. Drag the canvas (with the script on it) onto the slot and then from the dropdown select MyScriptName → MoveMeLeft for the Left button PointerEnter/PointerDown and well you get the idea just add each public function to the correct event on the button.

can anyone post the complete script.
I follow the instruction but could not run the it in my game

http://answers.unity3d.com/users/265138/mmmpies.html

it seems that my player takes sometime to switch between both left and right movements, how can i make it smoother and faster?
@mmmpies