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
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.");
}
}
how do you use this code to rotate a sphere on ios when the user drags their finger around the gui texture