Hello,
For my bachelor thesis im augmenting a physical paper map. Therefor im using unity in combination with vuforia for imagetarget detection and further functions. So far so good.
Now the problem: Im using cube elements that are augmented beside the map as interaction elements to filter the projected content. Those cubes have box colliders on em. I attached the following "buttonController" script to my AR camera which should handle the Raycast hits on those cubes and trigger further functions.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ButtonController : MonoBehaviour { public AudioClip[] audioClips; public AudioSource myAudioSource;
private string btnName; private GameObject[] stations;
private void Start() { myAudioSource = GetComponent<AudioSource>(); }
private void Update() {
if (Input.GetMouseButtonDown(0)) {
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 tapPositionFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane); Vector3 tapPositionNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
Vector3 tapPosF = Camera.main.ScreenToWorldPoint(tapPositionFar); Vector3 tapPosN = Camera.main.ScreenToWorldPoint(tapPositionNear);
int layerMask = LayerMask.GetMask("Button", "Pin"); RaycastHit hit;
if (Physics.Raycast(tapPosN, tapPosF - tapPosN, out hit, Mathf.Infinity, layerMask)) {
btnName = hit.transform.name; Debug.Log("NAME OF HIT TARGET: " + hit.transform.name);
myAudioSource.clip = audioClips[0]; myAudioSource.Play();
switch (btnName) { case "buttonRoute1": Debug.Log("In: CASE 1");
playAnimation(btnName); stations = GameObject.FindGameObjectsWithTag("Route1");
Debug.Log(stations);
LineRenderer lineRenderer = GetComponent<LineRenderer>();
foreach (GameObject station in stations) { MeshRenderer mRenderer = station.GetComponent<MeshRenderer>(); if (mRenderer.enabled == true) { mRenderer.enabled = false; } else { mRenderer.enabled = true; } } return;
case "buttonRoute2": Debug.Log("In: CASE 2"); playAnimation(btnName); return;
case "buttonRoute3": Debug.Log("In: CASE 3"); playAnimation(btnName); return;
case "buttonRoute4": Debug.Log("In: CASE 4"); playAnimation(btnName); return;
case "buttonRoute5": Debug.Log("In: CASE 5"); playAnimation(btnName); return;
case "buttonRoute6": Debug.Log("In: CASE 6"); playAnimation(btnName); return; } } else { //Debug.DrawRay(ray.origin, ray.direction * 1000, Color.white); Debug.Log("NOTHING HAPPENED WITH MOUSE"); } } }
void playAnimation(string btnName) { GameObject currentGameObject = GameObject.Find(btnName); Animation anim = currentGameObject.GetComponent<Animation>(); anim.Play(); } }
This cubes are set to special XZ coordinates in the unity scene and do not get moved programmatically during runtime in another script. I also augment pins which are placed on the 0/0/0 of an imagetarget and get repositioned during runtime to new XZ coordinates calculated by their LatLong coordinates. Their Raycast hits are detected aswell by the above script.
When i run my application in the editor everything works perfectly fine. Each of the elements, the cubes and the pins, get hit by the raycast exactly like they should.
So far so good.
When i build the android version and install it to my xiaomi android phone the raycasts dont hit like they should. I dont get any hit at all when i touch the cubes on their original position beside the map. BUT i get hits in the blue marked area seen in the picture below.
(See image 1 attached)
It looks like the boxcolliders of the cubes on the side get all move onto the 0/0/0 position of my imagetarget during runtime although the models keep their original position.
The pins dont get any hits at all although they have active mesh colliders on them and get hit in the editor too. Im extremely desperate now since ive already tried like each and every advice i found in thousands of threats.
I tried setting up a new AR camera element, move the script to another object, change hierarchy, change colliders, reset colliders, different raycast scripts from mouseclick to touch, use a different device, ... and so on.
What i just found out by debugging:
Like i already said i think the colliders move during the runtime. I debugged the colliders of buttonRoute 1 and buttonRoute 2 while hitting the blue marked zone from screenshot 1.
I get hits and the colliders position is completely different.
marked green and yellow Cubes are cubes to visualize the boxcolliders of Route 1 and Route 2 Button cube beside the map.
(See image 2 attached)
They are not even at the same wrong position. One is above the 0 of Y axis and one underneath. Their X and Z coordinates are completely wierd. They should keep the position of the Green Route 2 and Yellow Route 1 button 3d elements shown beside the map! I have NO idea why this happens....
Since im extremely desperate and dont know what else to do i d appreciate any kind of hint =(
Hi,
Hi,
Looking at your code, I can see that the entire execution of Void Update is based on the MouseButton input, based on this nothing will happen on the phone, as you have a touch input.
I mean you are right. Ive
I mean you are right. Ive read on the internet and several posts including the unity manual that the mouseButton event will automatically transform into a touchevent when running on android devices. And i do get hits. The Functions are working on my android phone.
Hi,
Hi,
If you get hits that it means that GetMouseBUtton works as expected.
You would need to check your code running on the buttons, something maybe is selecting by mistake the box colliders at moving them.