- Home /
Double Touch Tap to Select Android
Hi, I've been trying to find how to double tap an object to select it on an Android device, I think I have the double touch part down, but the actual selection of the object is what isn't working. I have it set to send a ray at the point of the tap. This works for single tap, but double tap it only sometimes works. The receiving end of the code just checks if I've double tapped on the object, then changes it's color. I have to tap 3 times sometimes, and even then it may not work, and it usually flickers (the color). The script doesn't seem quite right, but I have no idea how to fix it.
using UnityEngine; using System.Collections;
public class tapToEffectObject : MonoBehaviour { public float maxRayDist; private bool touched; public RaycastHit hitInfo;
// Use this for initialization
void Start () {
if(maxRayDist < 0.1f) {
maxRayDist = 1;
}
}
// Update is called once per frame
void Update () {
//single tap
if(Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Stationary )) {
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if(Physics.Raycast(ray, out hitInfo, maxRayDist)) {
touched = true;
hitInfo.transform.SendMessage("SingleTap");
Debug.Log(hitInfo.collider.tag);
}
else {
touched = false;
// Debug.Log("Nothing hit."); } }
foreach(Touch t in Input.touches) {
if(t.tapCount == 2) {
Ray ray = Camera.main.ScreenPointToRay(t.position);
if(Physics.Raycast(ray, out hitInfo, maxRayDist)) {
hitInfo.transform.SendMessage("DoubleTap");
Debug.Log(hitInfo.collider.tag);
}
}
}
}
void OnGUI() {
if(touched) {
GUI.Label(new Rect(10, 10, 100, 40), "YOLO");
}
}
}
My focus is on the foreach section where the double tap is. Should I have it send a ray for each touch? If so, I haven't been able to figure out how to do that.
It looks to me like your code is a little strange, you are setting touched to false every time you don't have a touch in an Update() so therefor it will only be true for very small periods of time. So that would cause your OnGUI() stuff to not really display since it's constantly set to false very quickly.
It's possibly I'm just not understanding what your code is trying to do here, not very clear to me if it is working correctly.
And yes you will want to cast a ray each time you want to assign an object or location with your tap.
This code just looks a bit off to me in general but I never used Input.touches I always have used:
if (Input.GetButtonDown ("Fire1"))
or:
if (Input.GetButton ("Fire1"))
Those could also work for you if you set up some kind of counter and IEnumerator function to reset the tap count to 0 if you don't have another tap in a specified time frame.
But perhaps someone else can give you details how to set this up right with the input touch stuff.
I myself am curious to see some good examples of using this stuff since it has never seemed necessary to me so far but I'm sure there are useful applications.
Hey thanks! This helps a lot. It's nice to have someone who tries to $$anonymous$$ch and have me find out ins$$anonymous$$d of just giving me the correct script. I'll try to use a counter function and see if that works. If not, I'll be back.
One thing I just noticed: touch is set to false if I touch again, so It doesn't reset until I tap again.
So I tried a new version of this with the IEnumerator and counter. I seem to be getting closer, and my code is a bit cleaner, but I'm still stuck. $$anonymous$$gestions? Also, when I just "cnt++;" when the ray hits, It adds too much. What would I do to have it only add once?
using UnityEngine; using System.Collections;
public class tapToEffectObject : $$anonymous$$onoBehaviour { public float maxRayDist; private bool touched; public RaycastHit hitInfo; public int cnt;
// Use this for initialization
void Start () {
if(maxRayDist < 0.1f) {
maxRayDist = 1;
}
}
// Update is called once per frame
void Update () {
foreach(Touch t in Input.touches) {
if(t.tapCount == 1) {
Ray ray = Camera.main.ScreenPointToRay(t.position);
if(Physics.Raycast(ray, out hitInfo, maxRayDist)) {
cnt = 1;
}
}
if(t.tapCount == 2) {
Ray r = Camera.main.ScreenPointToRay(t.position);
if(Physics.Raycast(r, out hitInfo, maxRayDist)) {
cnt = 2;
}
}
}
if(cnt == 1) {
StartCoroutine("TouchDelay1");
}
if(cnt == 2) {
StartCoroutine("TouchDelay2");
}
}
IEnumerator TouchDelay1() {
hitInfo.transform.Send$$anonymous$$essage("SingleTap");
yield return new WaitForSeconds(0.5f);
Debug.Log("Reset cnt to zero");
cnt = 0;
touched = false;
}
IEnumerator TouchDelay2() {
hitInfo.transform.Send$$anonymous$$essage("DoubleTap");
yield return new WaitForSeconds(0.5f);
Debug.Log("Reset cnt to one");
cnt = 1;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613114931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
i need help in walking character in android, using raycast where user touches? 0 Answers
Select and drag a gameObject with Touch 2 Answers
Get the position of the first finger that touched the collider? 1 Answer
Scripting to receive a touch over a specific area 0 Answers
Moving a 3D object with 2D swipes 2 Answers