x


Help with iphone/android touch controls

Hello i am learning android development i created a simple 2D car game with left arrow used to go left and right arrow to right. I use these commands

if (Input.GetKey(KeyCode.LeftArrow))

now that i am successful with it in pc i want it to use with my android. I created 2 guiText '<' for left and '>' for right i got them on the screen but i dont know how to implement them in my script and use it like 'player touches '>' for right' .. please help

more ▼

asked Jun 04 '11 at 07:38 AM

Anonymous 1 gravatar image

Anonymous 1
98 16 18 22

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

2 answers: sort voted first

Hi There!

For your entertainment and mine I created a simple script which should implement touch controls for phones (I don't have a mobile phone!)

Oh, also, whilst you 'could' use GuiText, a guiTexture is much more easier to use for mobile input (although neither is preferable!) Regardless, here's the code:

using UnityEngine;
[RequireComponent(typeof(GUITexture))]
public class UnityBasic1 : MonoBehaviour
{

private GUITexture _gui;
public Vector2 MousePosition;
private bool _buttonEnabled;//This means this button can only be 'pressed' once a frame.

void Start()
{
    transform.localScale = Vector3.zero;//for some retarded reason gui elements scale from 0 not 1.
    _gui = GetComponent<GUITexture>();
    _gui.pixelInset = new Rect(_gui.pixelInset.x, _gui.pixelInset.y, _gui.texture.width, _gui.texture.height);//make sure it's pixel perfect sized.
}
void Update()
{
    _buttonEnabled = false;//We reset the button.

    foreach (Touch touch in Input.touches)
    {
        if (_buttonEnabled) return;//don't go any further if this button is pressed.
        if (!_gui.HitTest(touch.position)) continue;//do hit test, we 'press' it??
        switch (touch.phase)
        {
            case TouchPhase.Began:
            case TouchPhase.Moved:
            case TouchPhase.Stationary:
                ButtonPressed();//We hit the button!
                return;//Return completely out of the update.
        }
    }

    //NOTE: PC TESTING
    if (_buttonEnabled) return;//don't go any further if this button is pressed.

    MousePosition = Input.mousePosition;
    if (!Input.GetMouseButton(0)) return;//Bugger off if no mouse press.
    if (!_gui.HitTest(MousePosition)) return;
    ButtonPressed(); //We hit the button!
}

private void ButtonPressed()
{
    _buttonEnabled = true;//This button has been hit.
    Debug.Log("Do incremental action here.");
}
}
more ▼

answered Jun 04 '11 at 10:43 AM

kariwmklawm gravatar image

kariwmklawm
61 4

dude i will sure check this when i get home , this community is best!!

Jun 04 '11 at 03:58 PM Anonymous 1

can u explain me how it works ?

Jun 05 '11 at 03:47 AM Anonymous 1

Hi, it uses a GuiTexture (1 drawcall per texture on screen btw), and uses the in-built function HitTest (for the GuiTexture) reading if you have your finger is on a point. If your finger is on a point inside a guiTexture (this includes any alpha around the edges of an opaque button), it will trigger a repeating (every update) function -- in this case ButtonPressed. Touches for a phone are read as Input.touches, which is an array that collects every touch you do. Each touch has different modes, and we're only interested in touches that are currrently on the screen. Each important line of code also explains what that line does.

Please be more precise if something is confusing in particular.

Jun 06 '11 at 12:10 AM kariwmklawm

thanx for your help but ( i am a little new ) how do implement this in my script ? like i said i use " if (Input.GetKey(KeyCode.LeftArrow))" how can i replace your code with mine ? thanx

Jun 06 '11 at 04:18 AM Anonymous 1

anyone ?? i am suffering

Jun 10 '11 at 11:58 AM Anonymous 1
(comments are locked)
10|3000 characters needed characters left

how do you use this code to rotate a sphere on ios when the user drags their finger around the gui texture

more ▼

answered Aug 03 '12 at 04:50 PM

awesomeface gravatar image

awesomeface
1 2 5

(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:

x2604
x2013
x1417
x606
x230

asked: Jun 04 '11 at 07:38 AM

Seen: 7524 times

Last Updated: Aug 03 '12 at 04:50 PM