- Home /
Android Tap detection
Hi,
I have a little bit odd issue with detecting touch inputs. I am trying to implement something like click with mouse, that is:
-player touches an object
-he does not move his finger
-when he lifts the finger, something happens (in my case level loads)
My code to achieve this is here:
var phase1 :boolean = false;
function Update () {
if(Application.platform == RuntimePlatform.Android) {
if(Input.touchCount > 0) {
var hit : RaycastHit;
var vektor = Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,0f);
//var fingerPos = Vector2(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y);
var ray : Ray = Camera.main.ScreenPointToRay (vektor);
if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
if(hit.collider.gameObject == gameObject) {
if (Input.GetTouch(0).phase == TouchPhase.Began){
phase1 = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Canceled){
phase1 = false;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended && phase1 == true && getPrevLevelCompleted(gameObject.name)){
phase1 = false;
loadingScreen.guiTexture.enabled = true;
PlayerPrefs.SetInt("lastPlayedLevel", getCurrentLevelNumber(gameObject.name));
Application.LoadLevel(gameObject.name);
}
}
}
}
}
}
The problem is, that this solution works on some devices, however on some devices not. For example on some cell phones player needs to tap on an item fast repeatedly several times to trigger an event. What do I do wrong? Is there some standard solution/function to implement TAP?
Answer by wolodo · Nov 22, 2012 at 10:36 PM
So I managed to solve it by myself. I modified my source code like this:
private var phase1 :boolean = false;
private var touchCoordinates :Vector2;
function Update () {
if(Application.platform == RuntimePlatform.Android) {
if(Input.touchCount > 0) {
var hit : RaycastHit;
var vektor = Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,0f);
//var fingerPos = Vector2(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y);
var ray : Ray = Camera.main.ScreenPointToRay (vektor);
if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
if(hit.collider.gameObject == gameObject) {
if (Input.GetTouch(0).phase == TouchPhase.Began){
phase1 = true;
touchCoordinates = Input.GetTouch(0).position;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved){
if(Vector2.Distance(touchCoordinates, Input.GetTouch(0).position) > 10) {
phase1 = false;
}
touchCoordinates = Input.GetTouch(0).position;
}
if (Input.GetTouch(0).phase == TouchPhase.Canceled){
phase1 = false;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended && phase1 == true && getPrevLevelCompleted(gameObject.name)){
phase1 = false;
loadingScreen.guiTexture.enabled = true;
PlayerPrefs.SetInt("lastPlayedLevel", getCurrentLevelNumber(gameObject.name));
Application.LoadLevel(gameObject.name);
}
}
}
}
}
}
Maybe it will be useful for someone.
Answer by CodeMasterMike · Nov 22, 2012 at 12:04 PM
Look at the documentation for GetTouch and you can see some "standard" sollution to how to use GetTouch.
A tip is that you loop through your touchCount and not just check the first one. Like the documentation says:
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
clone = Instantiate (projectile, transform.position, transform.rotation);
}
}
Good luck!
Thanks, but this doesn't seem to solve my problem. If I trigger an event at TouchPhase.Began, the user does not have a chance to cancel his tap. I want it similar like when you click on some html link or button with mouse. If you hold the button, nothing happens. If you release the button, the link is clicked however, if you push mouse button, wait some time or move away from the link, also nothing happens. (I now, there is missing any timer in my code, I will add it later) Shouldn't I add some latency to this action? For example tolerate some small movement of the finger?
Well in the documentation, you will find there is a phase called Ended, which works the same way as a release of a mouse button.
http://docs.unity3d.com/Documentation/ScriptReference/TouchPhase.Ended.html
So do only your GetTouch checks if the phase is equal to Ended. Then you don't need any extra checks or time delays.
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
// Do your whatnots here...
}
}
Yes, I know. I use it in my code. Still not sufficient. I need to have Begin phase on my object, not move and also Ended phase on the same spot.
I would do it like so, that when the phase is ended, then I would take the Touch.position and do a raycast/my own collision check with the position to see if the player has touched a object or not. The begin phase come automaticly so you wouldn't really need that one.
But if I had to use Begin and move as well, I would do something like this:
bool $$anonymous$$oved = false;
bool Began = false;
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Begin)
{
Began = true;
}
if (Input.GetTouch(i).phase == TouchPhase.$$anonymous$$oved)
{
$$anonymous$$oved = true;
}
if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
if(Began == true && $$anonymous$$oved == false)
{
// Do your whatnots here...
}
else
{
return;
}
}
}
Actually this code is almost same as $$anonymous$$e. Please take a look at it. $$anonymous$$y problem is, that it wasn't fully working. Especially with devices with displays with higher ppi. So I was wondering if most of the taps on these devices make also small movement. That could be the answer to my problem but I am not sure...