团结-对孩子的射线广播无法正常工作 [英] Unity - Raycast on child doesn't work

查看:98
本文介绍了团结-对孩子的射线广播无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用一些带有光线投射的游戏对象.

I want to enable some gameobjects with a raycast.

我有一个主要对象,它使一个Child与一个对撞机共同构成.该子项包含一个文本,当该子项的对撞机被射线投射击中时,我要启用该文本​​.我已经尝试使用下面的代码,但是没有用.如何通过射线广播访问孩子?

I have a principal object which cointains a Child with a collider. This Child contains a text which I want to enable when the Child's collider is hit by the raycast. I've tried with the code bellow but doesn't work. How can I access the child with the raycast?

public class SeleccionarTesoro_LIST : MonoBehaviour {
    void Start()
    {
        GameObject[] hitObject = GameObject.FindGameObjectsWithTag("TESOROS");
    }

    public void SetHitObjectToActive(GameObject hitObject)
    {
        hitObject.transform.GetChild(0).GetChild(0).gameObject.SetActive (true);
        hitObject.transform.GetChild(0).GetChild(2).gameObject.SetActive (true);
    }

    void Update() {

        if (Input.GetMouseButtonDown (0)) 
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

            if (Physics.Raycast (ray, out hit)) {
                SetHitObjectToActive (hit.collider.gameObject);
            } 
        }
    }

}

推荐答案

根据您的评论,我认为您试图以错误的方式与孩子取得联系.

Based on your comments, I thing you are trying to reach the child in a wrong way.

首先使用名称调用每个gameObject.例如:

Start by calling each gameObject with a name. For example:

  • MainGameObject
  • ChildCapsule
  • ChildText

现在,要访问孩子,请执行以下操作:

Now, to access the child do like this:

public void SetHitObjectToActive(GameObject hitObject)
{
     hitObject.transform.Find("ChildCapsule").gameObject.transform.Find("ChildText").gameObject.SetActive (true);
}

这篇关于团结-对孩子的射线广播无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆