-1

I have built an AR application using Unity and Vuforia. So basically when i scan something i see a model. I have removed the main camera and we are using AR camera for this purpose.

Now on running the apk on Mobile i want to open a url or show some message on touching the 3d model which comes after scanning the image.

I know it uses Raycasting but a code snippet which can help me in opening a url on clicking 3d model would help. I am a beginner in Unity so the help would be appreciated a lot.

Rocker Rocky
  • 97
  • 4
  • 16

1 Answers1

1

Your models need to have colliders attached to them in order for the raycast to register the collision. Also it's convenient to use tags to filter out the raycast results. You can use layer masks for the same purposes though. Input.touches return an array of all registered touches during last frame, let's assume that there is only one touch registered, but if you want you can iterate through all the touches in case there are more than one and check if any of them hit the model. Then, you can do something like this:

    public void RegisterModelTouch()
    {
        // We assume that there was only one touch and take the first 
        // element in the array.
        Touch touch = Input.touches[0];
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.CompareTag("YourModelTag"))
            {
                // Do something (open an URL in your case).
            }                
        }
    }

Hope this helps.

Marko
  • 34
  • 4
  • sorry for such easy questions. How can i get to know the YourModelTag. Can you help in that. And also can you let me know since there is no Main Camera as i have removed it and i am using AR Camera so will Camera.main will work. Waiting for your valuable inputs – Rocker Rocky Jun 22 '17 at 11:58
  • You can create your own tags and then assign them to game objects I just put "YourModelTag" as an example. Check this [page](https://docs.unity3d.com/Manual/Tags.html) out for an in-depth explanation. As for the camera, Camera.main will return any camera in the scene that is tagged: MainCamera. So just select that tag for your AR Camera and that's it. – Marko Jun 22 '17 at 12:11
  • And we have to add this script to the AR camera and 3d model both or only to the 3d model – Rocker Rocky Jun 22 '17 at 12:13
  • And for camera also i need to make one tag like for game object and use it as Camera.CameraTagName where CameraTagName is the tag assigned to camera. Or i can just tag the AR camera as MainCamera – Rocker Rocky Jun 22 '17 at 12:14
  • The MainCamera tag is a built-in Unity tag, so you don't need to create a tag specifically for the camera, just select your AR camera and in the upper left corner of the inspector click on tags and select the MainCamera tag. You do need to create a tag for your model, just click add tag and name however you want. Then in the ComperaTag() just write in the name of the that tag. – Marko Jun 22 '17 at 12:21
  • You can attach the script to your camera or your model, whatever is convenient for you, but don't attach it to both. Basically you need to call the method in the Update() so that every frame we first check if any touches occurred and if they did, we check if you tapped on the model. void Update() { // If any touch occurred during this frame. if(Touches.count > 0) { RegisterModelTouch(); } } – Marko Jun 22 '17 at 12:25
  • Sure @Marko i will check this right now and will update you. Thanks for your valuable inputs. Will update you in some 10 mins. – Rocker Rocky Jun 22 '17 at 12:27
  • No problem man. hope it helps. :) I'm here if you have any more questions. – Marko Jun 22 '17 at 12:29
  • Thanks @Marko it works wonders. Can you also let me know how can i detect no of touces. Like i want to do something on double tap. – Rocker Rocky Jun 22 '17 at 13:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147377/discussion-between-marko-and-rocker-rocky). – Marko Jun 22 '17 at 14:11
  • Just a quick note that I was having trouble detecting touches until I realized my ARCamera needed a Physics Raycaster component added. The above works great with that in place. – Jon K Jul 15 '18 at 19:12