x


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);
    }
}
more ▼

asked May 11 '12 at 01:06 AM

Ranger_Steve gravatar image

Ranger_Steve
0 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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();
                }
            }
        }
    }
more ▼

answered May 12 '12 at 05:45 PM

alpaca of zion gravatar image

alpaca of zion
126 1

I can't seem to get this script to work. I don't know if it's the fact the I am referencing something wrong, but what I have done now is created a GUI Text object and added that to the script. I also added it to the main camera so instead of guiCamera I have Camera.main but there is no movement what so ever.

May 15 '12 at 02:34 AM Ranger_Steve
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3748
x2491
x957
x143
x65

asked: May 11 '12 at 01:06 AM

Seen: 1472 times

Last Updated: May 15 '12 at 02:34 AM