zoukankan      html  css  js  c++  java
  • Unity3D中的射线与碰撞检测代码

    两种不同写法的射线检测

    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);
      }

    }

  • 相关阅读:
    畅通工程续
    find the safest road
    Window Pains
    什么是DO / DTO / BO / VO /AO ?
    编程四大件
    1.Redis简介和安装
    0.Redis课程大纲
    8.docker容器虚拟化与传统虚拟机比较
    7.docker私有仓库
    6.Docker服务编排
  • 原文地址:https://www.cnblogs.com/Study088/p/7363935.html
Copyright © 2011-2022 走看看