1.获取鼠标点击的物体

if (Input.GetMouseButtonDown(0))
{
  Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition); //以摄像机为原点创建一条射线
  RaycastHit hit;
  if (Physics.Raycast(ray, out hit)) //点击到了带有碰撞体的物体
  {

    Transform clickObjTrans = hit.transform; //获取点击的物体

    if (clickObjTrans.name == "Manager")
    {
      
    }

     Debug.DrawLine(ray.origin, hitInfo.point,Color.red);//画出射线

  }

}

2.获取鼠标点击的物体

if (Input.GetMouseButtonDown(0))
{
  // 获取鼠标点击位置

  //创建射线;从摄像机发射一条经过鼠标当前位置的射线

  Ray ray = firstCamera.ScreenPointToRay(Input.mousePosition);
  RaycastHit hitInfo = new RaycastHit(); //发射射线
  if (Physics.Raycast(ray, out hitInfo))
  {
    //获取碰撞点的位置
    if (hitInfo.collider.name == "Ground")
    {
     
    }
    Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
  }

}

 

相关文章:

  • 2022-12-23
  • 2021-11-11
  • 2021-09-07
  • 2021-03-30
  • 2021-08-06
  • 2022-12-23
猜你喜欢
  • 2021-06-12
  • 2021-09-30
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2019-08-01
相关资源
相似解决方案