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

    }

  • 相关阅读:
    Delphi防止同时出现多个应用程序实例CreateMutex
    DLL注入代码
    DLL注入代码
    C语言学习笔记
    随笔
    存储器简介
    随笔
    对偶问题的基本性质
    C语言学习笔记
    对偶问题的基本性质
  • 原文地址:https://www.cnblogs.com/Study088/p/7363935.html
Copyright © 2011-2022 走看看