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 ronnyvdb · Jun 30, 2014 at 02:57 PM · androidtouchvuforiaraycasthit

Unexpected behavior on touch android & vuforia

I have a scene that uses Vuforia AR SDK to recognize an image target. Once the target has been recognized I enable the renderer of a GameObject of the same name, with a prefix.

When the user touches the GameObject I want the GameObject to lerp to a position at the center of the screen, and when touched again, it should return to it's original position.

When I play the scene in the Unity Player everything works perfectly and I get the behavior that I expect. However, when I build and run the scene on Android, when I touch the GameObject nothing happens, but when I touch anywhere outside of the GameObject the lerping effect runs.

I dont understand why I am getting different behaviors between the Unity Player and on Android???

I have two scripts. the TouchDown script is attached to the AR Virtual camera, and the TouchListener is attached to the GameObject(s) that should be touched.

TouchDown: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class TouchDown : MonoBehaviour
 {
     
     void Update () {
         
         RaycastHit hit = new RaycastHit();
         for (int i = 0; i < Input.touchCount; ++i) {
             if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
                 // Construct a ray from the current touch coordinates
                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                 if (Physics.Raycast(ray, out hit)) {
                     hit.transform.gameObject.SendMessage("OnMouseDown");
                 }
             }
         }
         
     }
 }

TouchListener: using UnityEngine; using System.Collections; using System;

 public class TouchListener : MonoBehaviour {
     string obj_name;
     bool moved = false;
     // Use this for initialization
     void Start () {
 
         string[] separators = {"_"};
         string value = this.gameObject.name;
         string[] values = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
 
         obj_name = values[1];
 
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     void OnMouseDown(){
 
         Debug.LogWarning("WE HAVE TOUCH ON: "+obj_name);
 
 
         Debug.LogWarning("TARGET NAME: "+obj_name);
 
         GameObject GameCard = GameObject.Find ("Card_"+obj_name);
         GameObject target;
         Vector3 sourcePos;
         Vector3 targetPos;
 
         if (moved) {
             Debug.LogWarning("TARGET NAME: "+obj_name);
             target = GameObject.Find ("CardHolder_"+obj_name);
 
             sourcePos = GameCard.transform.position;
             targetPos = target.transform.position;
             //Debug.LogError("X: "+target.transform.position.x+", Y: "+target.transform.position.y+", Z: "+target.transform.position.z);
 
             moved = false;
         } else {
             target = GameObject.Find ("CardPosCenter");
             sourcePos = GameCard.transform.position;
             targetPos = target.transform.position;
             moved = true;
         }
 
         Quaternion sourceRot = GameCard.transform.rotation;
         Quaternion targetRot = target.transform.rotation;
 
         StartCoroutine(MoveCard (target, sourcePos, targetPos, sourceRot, targetRot, 0.5f));
 
     }
 
     IEnumerator MoveCard(GameObject targetObj, Vector3 source, Vector3 target, Quaternion sourceRot, Quaternion targetRot, float overTime)
     {    
         GameObject GameCard = GameObject.Find ("Card_"+obj_name);
         
         float startTime = Time.time;
         while(Time.time < startTime + overTime)
         {
             GameCard.transform.position = Vector3.Lerp(source, target, (Time.time - startTime)/overTime);
             GameCard.transform.rotation = Quaternion.Lerp(sourceRot, targetRot, (Time.time - startTime)/overTime);
             yield return null;
         }
 
 
         GameCard.transform.position = target;
 
         if (!moved) {
 
             Vector3 temp = new Vector3(targetObj.transform.localPosition.x, targetObj.transform.localPosition.y, 997.1f);
             GameCard.transform.localPosition = temp;
         }
 
         GameCard.transform.rotation = targetRot;
 
 
     }
 }

I would really appreciate any suggestions on how to get the touch events working on GameObjects. TIA!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by moe85 · Dec 27, 2014 at 07:58 PM

i faced the same issue, i guess if you try to touch the object using 2 fingers would work, to remove this you have to add touch correction as following :

 void Update () {
 

     int TouchCoreection = 1;
     RaycastHit hit = new RaycastHit();
     for (int i = 0; i+TouchCoreection < Input.touchCount; ++i) {
         if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
             // Construct a ray from the current touch coordinates
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
             if (Physics.Raycast(ray, out hit)) {
                 Debug.Log(this.name);
                 hit.transform.gameObject.SendMessage("OnMouseDown");            
             }
         }
     }
 }


let me know if it is works :)

Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with touchscreen controls 2 Answers

touch position to world position 2D 2 Answers

Android 3D Touch for Menu 0 Answers

Unity 2D Mobile Game Drawing Mechanic 0 Answers

How to touch, move and rotate 3D objects in XZ? 1 Answer


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