- Home /
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!
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 :)
Your answer
Follow this Question
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