Log in or register to post comments

No Raycasthit in the cube target detected

January 8, 2020 - 1:53pm #1

Hey peoples,

 

I'm an absolute beginner with Unity and Vuforia. For my Bachelor thesis, I'm working on an AR Application augmenting a physical 2D Map. For filtering the content I tried to use some 3D buttons on the side which execute different filter options.

On youtube, I found several videos to setup raycast to track model hits through touch interaction. For testing, i copied the code from the video.

 

I attached the script as he does in his video (https://www.youtube.com/watch?v=hi_KDpC1nzk) to my image target.

My 3D Button (Cube) is a child of it with a mesh collider on it.

 

Whatever I do my camera / android device does not detect any hits at all. For reasons I never get into the needed part of the function cause (Physics.Raycast(ray, out hit) never returns true...

I am absolutely clueless to what else I could do to make this work. I`ve already tried each advice I found on several forums and or videos.

 

This is the code I use:

 

using UnityEngine;

using UnityEngine.Events;

using UnityEngine.EventSystems;

public class buttonController : MonoBehaviour

{

    public AudioClip[] audioClips;

    public AudioSource myAudioSource;

    string btnName;

    private void Start()

    {

        myAudioSource = GetComponent<AudioSource>();

    }

    private void Update()

    {

        if (Input.touchCount > 0 && Input.touches [0].phase == TouchPhase.Began)

        {

            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))

            {

                btnName = hit.transform.name;

                switch (btnName)

                {

                    case "buttonPOI":

                        myAudioSource.clip = audioClips[0];

                        myAudioSource.Play();

                        break;

                }

            } else

            {

                Debug.Log("NOTHING HAPPENED");

            }

        }

    }

}

 

 

I would appreciate it so much if anyone has a clue or hint.

 

Regards

Stephan

No Raycasthit in the cube target detected

January 10, 2020 - 7:13am #8

EDIT:

Ok i got the things working in a complete new Project with nothing in it but the elements and scripts he used in his video.

 

But its not working in the project ive set up for my BA although the only difference is that i use 9 imagetargets instead of 1 and several 3D model children in each of them instead of only the Button element. (see attached img)

 

For now im trying to make it work for Imagetarget 1_1 only. Ive put the exact script on the exact same element as i did in den new project and everything else should be exactly the same aswell.

 

 

AttachmentSize
Image icon img.PNG9.5 KB

No Raycasthit in the cube target detected

January 10, 2020 - 3:34am #7

Ok nvm, i got the debugging in Terminal running properly now.

 

 

So:

 

Debug.Log("Conditions for raycase have been fullfilled") is getting logged in the console which means the touch input seems to be registered. 

BUT the Debug.Draw is not showing anything .

 

The if(physics. ...) function still doesnt return TRUE which means i always and up in the else "NOTHING HAPPENED" part.

No Raycasthit in the cube target detected

January 10, 2020 - 3:08am #6

I have a problem to Debug.log from my via USB connected Android device to unity console. Any advice how to do that?

No Raycasthit in the cube target detected

January 10, 2020 - 1:46am #5

First of all thanks for the quick answers!

 

I will try out your advice immediatly and give you feedback what happens or doesnt happen.

No Raycasthit in the cube target detected

January 10, 2020 - 1:16am #4

Hi,

I would recommend to the following:

1. First make sure that the ray is being generated, add below 2 lines after the if condition:

...

if (Input.touchCount > 0 && Input.touches [0].phase == TouchPhase.Began)

        {

            Debug.Log("Conditions for raycase have been fullfilled"); ---> DEBUG IT

            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

            Debug.DrawRay(ray.origin, ray.direction *20, Color.Red); -----> WILL DRAW THE RAY

...

2. Add only Box colliders to the Cube

 

Try the above and let me know the outcome.

Thank you.

Vuforia Engine Support

No Raycasthit in the cube target detected

January 9, 2020 - 11:36pm #3

Hey mcotora,

I'm using a mesh collider on the cube.

I think on creating the cube there has been a box collider on it as well. Sadly neither nor helped

No Raycasthit in the cube target detected

January 9, 2020 - 4:15am #2

Hi,

Do the 3d cubes have box coliders setup?

Thank you.

Vuforia Engine Support

Log in or register to post comments