Android Input

Hey, I have been trying to get touch screen controls working for awhile now. But I just can’t get it to work the way I want it to. I have one gui button at the moment to move to the right, but when I touch the button the cube only moves slightly and I have to keep pressing the button to move any further. Is there away to get this to move constantly when I touch down on the button and then stop when it’s not touched? the only way I got this working was by using the Input.touchCount >= 1 but that will only work for one direction can anyone help me? please.

here is my code after a few attempts

//Vectors for movement directions
private Vector3 left,right;

//Float for speed
private float speed;

//Boolean to check if button is touched
private bool touched;

//Setting variables initial values
void Start () 
{
	touched = false;
	speed = 2f;

    //Values for movement along the X-axis
	left = new Vector3(-speed,0,0);
	right = new Vector3(speed,0,0);
}

void Update ()
{	
    //calling the movement method
	applyMovement();
}

private void OnGUI()
{
    //If the button and the screen is touched set the touched boolean to true 
	if(GUI.Button(new Rect(10,10,150,150),"RIGHT") && Input.touchCount >= 1)
	{
		touched = true;
	}
	
	else
	{
		touched = false;
	}
}

private void applyMovement()
{
	if(touched)
	{
        //apply positive movement along the x-axis when touched
		transform.Translate(right);
	}
}

GUI.Button(new Rect(10,10,150,150),“RIGHT”) only returns true when the button is first clicked.

One possible solution would be to skip the OnGUI and make actual game objects the user can click. I’m doing this with a second camera showing a gui layer. In the Update() function your code, attached to the button gameobject, would look something like:

    // Loop through the touches looking for a finger on the right button.
    for (int t = 0; t < Input.touchCount; t++)
    {
        if (Input.touches[t].phase == TouchPhase.Began || 
            Input.touches[t].phase == TouchPhase.Moved || 
            Input.touches[t].phase == TouchPhase.Stationary)
        {
            inputPosition = Input.touches[t].position;
            Ray ray = guiCamera.ScreenPointToRay(inputPosition);
            RaycastHit hitInfo = new RaycastHit();
            if (Physics.Raycast(ray, out hitInfo, 100))
            {
                if (hitInfo.collider.gameObject == this.gameObject)
                {
                    YourCharacter.MoveRight();
                }
            }
        }
    }