提问者:小点点

使用光线投射与放置在Vuforia接地平面上的3D对象交互


你好,所以我一直在寻找解决我的问题的方法,但看起来好像什么都没有。

我正在处理一个场景,在这个场景中,我在地平面上渲染了一些3D对象,我的目标是在那个3D对象上制作动画,首先点击它。我正在使用最新版本的vuforia 10.4和Unity 2020.3。9f1。我有一个脚本,用于实例化3d模型,并使“平面查找器”消失,只要它不丢失跟踪`使用系统。收藏;使用系统。收藏。通用的使用UnityEngine;

公共类的场景管理器:MonoBe行为{私有字符串级别="蛇";

public GameObject[] renderredPrefab;
public GameObject ground;
public GameObject groundFinder;

private int levelChecker(string levelName)
{
    if (levelName == "snake")
        return 0;

    else return 1;
}

public void spawnObject(int i)
{
    Instantiate(renderredPrefab[levelChecker(level)], new Vector3(0, 0, 0), Quaternion.identity, ground.transform);
}

public void groundFinderOff()
{
    groundFinder.SetActive(false);
}

public void groundFinderOn()
{
    groundFinder.SetActive(true);
}

}和另一个使用System.集合;使用System.收藏。通用;使用UnityEngine;

公共类动画师:单行为{私人动画师m_Animator;

private string objectName;
private GameObject[] eos;
private GameObject snake;

[SerializeField]
private Camera ARCamera;

// Start is called before the first frame update
void Start()
{
    // Get the different eos present on the scene
    for (int i = 0; i < eos.Length; i++)
    {
        eos[i] = GameObject.FindWithTag("myEolienne" + i);
    }

    // Get snake game objecct in the scene
    snake = GameObject.FindWithTag("Snake");
}

// Update is called once per frame
void Update()
{
    if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    {
        Ray ray = ARCamera.ScreenPointToRay(Input.GetTouch(0).position);

        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            objectName = hit.collider.name;
            Debug.Log("raycast touched " + hit.transform.name);
            switch (objectName) //Get the Animator depending on which gameObject been tapped on.
            {
                case "myEolienne1":
                    m_Animator = eos[0].GetComponent<Animator>();
                    // Launch the animation on the gameObject that's been tapped 
                    m_Animator.SetTrigger("Helice_Rotate");
                    Debug.Log("rotate launched");
                    break;

                case "myEolienne2":
                    m_Animator = eos[1].GetComponent<Animator>();
                    // Launch the animation on the gameObject that's been tapped 
                    m_Animator.SetTrigger("Helice_Rotate");
                    Debug.Log("rotate launched");
                    break;

                case "myEolienne3":
                    m_Animator = eos[2].GetComponent<Animator>();
                    // Launch the animation on the gameObject that's been tapped 
                    m_Animator.SetTrigger("Helice_Rotate");
                    Debug.Log("rotate launched");
                    break;
                case "Snake":
                    m_Animator = snake.GetComponent<Animator>();
                    m_Animator.SetTrigger("snakeMoving");
                    break;
            }
        }
    }
}

} `

请注意,每个3D模型都有不同的部分分组在一个父级中,父级only.enter图像描述上有一个网格碰撞器

渲染效果非常好,但我无法找出光线投射脚本的错误。注意,我第一次尝试在图像目标上使用3D模型,效果很好。

提前感谢!


共1个答案

匿名用户

使用

private void OnMouseDown()
{
 ...   
}

而不是光线投射,当然除非对撞机有重量