简体   繁体   English

Vuforia SDK在Unity3d中使用Raycast for ARCamera的问题

[英]Problems with Raycast for ARCamera in Unity3d with Vuforia SDK

I am new to Vuforia. 我是Vuforia的新手。

The gameobject to which the script is added, is a 3d object which is made visible on user-defined triggered image. 添加了脚本的游戏对象是一个3d对象,在用户定义的触发图像上可见。

I know this is not a new question and I have gone through each of the thread/post on official Vuforia discussion blog for that matter but the problem still persists. 我知道这不是一个新问题,因此我已经遍历了Vuforia官方讨论博客上的每个主题/帖子,但问题仍然存在。 And the problem seems very fundamental. 这个问题似乎非常根本。

I have the following script attached to my gameobject : 我的游戏对象附有以下脚本:

void Update () 
{
    if (Input.touchCount == 1) 
    {
        // Touches performed on screen
        Ray ray;
        RaycastHit hit;
        Debug.Log ("2");
        if(Camera.main != null)
        {
            Debug.Log ("3");
            ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            hit = new RaycastHit();
            Debug.Log ("33");
            if(Physics.Raycast(ray, out hit))
            {
                Debug.Log ("4");
            }
                   }
            }
    }

When I run the scene and touch on the gameobject, the Debug Console shows 当我运行场景并触摸游戏对象时,调试控制台将显示

2 2

3 3

33 33

BUT NOT 4. Somehow this ray doesn't hit the object. 但不是4.射线无法照到物体上。

This script works fine with the normal camera. 该脚本可以在普通相机上正常工作。 Could anyone please shed some light on this. 任何人都可以对此有所了解。

Thanks 谢谢

(as far as I can tell) Vuforia doesn't use the ARCamera for collision detection. (据我所知),Vuforia没有使用ARCamera进行碰撞检测。 Instead there is another 'Background Camera' (you can see it if you run your app in Unity and pause it; you'll find it in the Hierarchy pane). 而是有另一个“背景摄影机”(如果您在Unity中运行您的应用程序并将其暂停,则可以看到它;可以在“层次结构”窗格中找到它)。 To access it use 要使用它

Camera.allCameras[0] 

instead of 代替

Camera.main

Hope that helps 希望能有所帮助

I think this is a bug between Collider class and ARCamera, but the solution is this: 我认为这是Collider类和ARCamera之间的错误,但是解决方案是这样的:

  1. Create a new Scene 创建一个新场景
  2. Create a Cube or any gameobject with collider component. 创建具有碰撞组件的多维数据集或任何游戏对象。
  3. NOT delete the Cube for any reason 请勿出于任何原因删除多维数据集
  4. Test with any hit algorithm(touch or mouse) 使用任何点击算法(触摸或鼠标)进行测试

     using System.Collections; using UnityEngine; public class rayoPrueba : MonoBehaviour { void start () {print("entro"); } void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, 100)) print("Si le jue"); } } 
  5. Replace the mainCamera for ARCamera 为ARCamera更换mainCamera

  6. Test Again 再次测试
  7. Put u`r Cube inside ImageTarget and the real model 将多维数据集放入ImageTarget和真实模型中
  8. Delete the Cube and lets dance!! 删除多维数据集,然后跳舞!! I dont know why, but the bug is killed with this ...(Y) .. 我不知道为什么,但是这个错误被杀死了。

The trick is... Never lose a gameObject with Collider Component from the scene.. 诀窍是...永远不要从场景中丢失带有Collider Component的gameObject。

如果尝试在3D模型上使用RayCast命中目标,则应确保在3D模型上添加Box Collider Component。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM