- Home /
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
Answer by kariwmklawm · Jun 04, 2011 at 10:43 AM
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.");
}
}
dude i will sure check this when i get home , this community is best!!
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.
thanx for your help but ( i am a little new ) how do implement this in my script ? like i said i use " if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))" how can i replace your code with $$anonymous$$e ? thanx
Answer by awesomeface · Aug 03, 2012 at 04:50 PM
how do you use this code to rotate a sphere on ios when the user drags their finger around the gui texture
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How to drag object along with dragging touch? 1 Answer
How to make a character move towards a side of the screen that's pressed at a constant rate? 2 Answers
Touch Hold for Power 1 Answer
I need to move an object when touched on an android phone 1 Answer