简体   繁体   English

Unity:Raycast 未检测到父对象的标签

[英]Unity: Raycast not detecting parent object's tag

I'm attempting to detect when a player wants to open/close a door so I create an emptyObject called Door Hinge that has the tag "Door" .我试图检测玩家何时想要打开/关闭一扇门,所以我创建了一个名为Door Hinge空对象,其标签为"Door" I then created a cube object named Door Body which is a child of Door Hinge and gave it no tag but did give it a layer of Ignore Raycast .然后我创建了一个名为Door Body立方体object ,它是Door Hinge的一个子节点,没有给它标签,但确实给了它一层Ignore Raycast I have the scaling of the parent set to (1, 1, 1) but did change the scaling of the child as well as its x position slightly.我将父级的缩放设置为 (1, 1, 1) 但确实稍微改变了子级的缩放以及它的 x position。

I'm not sure why but the raycast seems to only be detecting the child cube and not the parent empty object.我不确定为什么,但光线投射似乎只检测到子立方体而不是父空 object。 Could anyone let me know if I'm missing anything or doing something wrong?如果我遗漏了什么或做错了什么,谁能告诉我? I'll add my detection code for this below.我将在下面添加我的检测代码。

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;
            }
        }
    }
}

The solution that I was missing was that the parent empty object needed to have a box collider added to it.我缺少的解决方案是父空 object 需要添加一个盒子对撞机。

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

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