繁体   English   中英

射线广播命中无法正常工作

[英]Raycast hit dont work properly

我已经编写了代码以通过选择对象来更改其颜色

void Update () {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
        if(Physics.Raycast(ray,out hit, 1000.0f) && Input.GetMouseButtonDown (0))
        {
            if(hit.collider.gameObject == this.gameObject)
            {
                Debug.Log("Wall Clicked");

                mgr.clickedWall=gameObject;

        }
        else if(Physics.Raycast(ray,out hit, 1000.0f) && Input.GetMouseButtonDown (1)) 
        {

            hit.collider.gameObject.renderer.material.color = Color.red;

        }
    }

当主摄像头位于初始位置但摄像头位置改变颜色但不单击我单击的对象时,它工作正常。这里的问题是什么。

我觉得您的整体逻辑是错误的。 你这样出来。 请注意,它是附加在相机上的,而不是您单击的游戏对象。 我认为您的问题源于是否在进行raycast时是否还检查了鼠标按钮是否按下。

 void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject.tag == "Test")
                {
                    Debug.Log("Wall Clcked");
                }
            }

        }
        if (Input.GetMouseButtonDown(1))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                hit.collider.gameObject.GetComponent<Renderer>().material.color = Color.red;
            }

        }
    }

暂无
暂无

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

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