繁体   English   中英

C#unity使用射线投射命中信息更改变量

[英]C# unity Changing a variable using ray cast hit information

此游戏中的相机的目标可以通过单击其他模型来更改,然后将成为相机的焦点,下面的脚本是我到目前为止所获得的内容,但是每次我单击游戏中的对象时,目标都不会显示任何内容而不是任何模型。

Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool didHit = Physics.Raycast(toMouse, out hitInfo);

if (didHit)
{
    if (hitInfo.collider.tag == "Cell" && Input.GetMouseButtonDown(0))
    {
        Debug.Log("Cell hit");

        target = hitInfo.transform.Find(gameObject.name);       
    }
}

如果此脚本在相机上,则应执行以下操作:

GameObject target;
// or
Transform target;

void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, out hit))
        {
            target = hit.transform.gameObject;
            // or
            target = hit.transform;
        }
    }   
}

暂无
暂无

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

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