射线检测3D物体

首先3d相机上加上physics Raycaster这个组件

Ray ray;
    RaycastHit rayhit;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ray =Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out rayhit, 10f))
            {
                Debug.Log(rayhit.collider.gameObject.name);
                if (rayhit.collider.gameObject.name =="Cube")
                {
                    Debug.Log(rayhit.collider.gameObject.name);
                }

            }
        }
    }

你可能感兴趣的:(射线)