Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Shruzikan1 · Sep 28, 2013 at 12:23 AM · raycasttouchandriod

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanZimmerman87 · Sep 28, 2013 at 12:23 AM 0
Share

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.

avatar image Shruzikan1 · Sep 28, 2013 at 04:53 PM 0
Share

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.

avatar image Shruzikan1 · Sep 28, 2013 at 04:56 PM 0
Share

One thing I just noticed: touch is set to false if I touch again, so It doesn't reset until I tap again.

avatar image Shruzikan1 · Sep 28, 2013 at 06:01 PM 0
Share

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;
 }

}

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges