射线点击物体

ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Input.GetMouseButton(0))
         {
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 if (hit.transform.tag == "cube")
                 {
                     //调试画出的射线
                    Debug.DrawLine(transform.position, hit.point);
                     //观察射线碰撞点
                     Debug.Log(hit.point);
                    //实现鼠标点击不松开,物体跟随鼠标移动,但是效果不怎么好
                     obj.transform.position = new Vector3(hit.point.x, hit.point.y, obj.transform.position.z);
                     //点击变色,就是为了提示哪个被点击了
                    hit.transform.renderer.material.color = Color.red;                }
             }

 

posted @ 2021-06-05 16:36  All_Nighter  阅读(43)  评论(0编辑  收藏  举报