【问题标题】:Unity: Raycast not detecting parent object's tagUnity:Raycast 未检测到父对象的标签
【发布时间】:2021-07-20 15:17:54
【问题描述】:

我试图检测玩家何时想要打开/关闭一扇门,因此我创建了一个名为 Door HingeemptyObject,它的标签为 "Door "。然后我创建了一个名为 Door Bodycube 对象,它是 Door Hinge 的子对象,并且没有给它标签,但确实给了它一层 忽略光线投射。我将父级的缩放设置为 (1, 1, 1) 但确实稍微改变了子级的缩放以及它的 x 位置。

我不知道为什么,但是光线投射似乎只检测到子立方体而不是父空对象。如果我遗漏了什么或做错了什么,谁能告诉我?我将在下面添加我的检测代码。

void CheckInteraction()
{
    // origin starts from the camera
    Vector3 origin = cam.transform.position;
    // direction of the camera
    Vector3 direction = cam.transform.forward;
    // The distance for the raycast
    float distance = 4f;
    // Used to store info about the object that the raycast hits
    RaycastHit hit;

    if (Physics.Raycast(origin, direction, out hit, distance))
    {
        Debug.Log(hit.transform.tag);
        if (hit.transform.tag == "Door")
        {
            Debug.Log("HIT");
            if (Input.GetKeyDown(KeyCode.E))
            {
                hit.transform.gameObject.GetComponent<DoorOpen>().enabled = true;
            }
        }
    }
}

【问题讨论】:

  • 你的父游戏对象有碰撞器吗?
  • 不...我现在感觉很愚蠢,哈哈,我还是 Unity 的新手,这是我第一次尝试使用 Raycasting。感谢您的评论,这就是我所缺少的!

标签: c# unity3d raycasting


【解决方案1】:

我缺少的解决方案是父空对象需要添加一个盒子碰撞器。

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2015-06-09
    • 2021-05-22
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多