射线记录

/// <summary>
    /// 工作面进度座标,传进去二维座标进行定位是否映射到巷道上
    /// </summary>
    public void WorkingFaceCoordinatesPos(List<Vector2d> vector2s)
    {
        List<Vector3> posList = new List<Vector3>();
        Vector3 vector = Vector3.zero;
        for (int i = 0; i < vector2s.Count; i++)
        {
            //减掉相对中心点
            Vector2 pos1 = (vector2s[i] + new Vector2d(CustomDataManager.SceneCenter.x, CustomDataManager.SceneCenter.z)).ToVector2();
            Vector3 pos = new Vector3(-pos1.x, -520, -pos1.y);
            posList.Add(pos);
            vector += pos;
        }

        //得到中心点
        vector = vector / posList.Count;

        for (int i = 0; i < posList.Count; i++)
        {


            //起点
            Vector3 origin = posList[i];

            //距离
            float dis = Vector3.Distance(origin, vector);

            //方向
            Vector3 direction = (vector - origin).normalized * 1f;

            //步数
            float passed = 0;

            while (passed < dis)
            {
                Debug.Log(passed + "::" + dis);
                Ray ray = new Ray(origin, Vector3.down);
                Debug.DrawRay(origin, Vector3.down, Color.red, 10f);
                if (Physics.Raycast(ray, out RaycastHit hit))
                {
                    if (hit.collider.gameObject.tag == TagManager.workingFace)
                    {
                        //y = hit.point.y;
                        Debug.Log("获取到点了" + hit.point.y);
                         GameObject.FindGameObjectWithTag(TagManager.WorkingFaceMesh).GetComponent<PolygonDrawer>().vertices.Add(new Vector3(posList[i].x, hit.point.y, posList[i].z));
                        dis = 0;
                        continue;
                    }
                    Debug.DrawLine(origin, origin + direction, Color.red, 10f);
                }
                origin += direction;
                passed += 1f;
            }


            //for (int i = 0; i < posList.Count; i++)
            //{
            //    GameObject.FindGameObjectWithTag(TagManager.WorkingFaceMesh).GetComponent<PolygonDrawer>().vertices.Add(new Vector3(posList[i].x, y, posList[i].z));
            //}

            GameObject.FindGameObjectWithTag(TagManager.WorkingFaceMesh).GetComponent<PolygonDrawer>().Draw();
        }

射线检测的坑

有时侯我们想在射线检测的时候忽略掉某些物体的碰撞,从而达到检测物体背后的物体的碰撞,这是个时候我们使用layerMask

layerMask参数使用按位与<<设置的一些总结:

  1 << 10 打开第10的层。
~(1 << 10) 打开除了第10之外的层。
~(1 << 0) 打开所有的层。
(1 << 10) | (1 << 8) 打开第10和第8的层。

 Debug.DrawLine(ray.origin, hitInfoPoint.point, Color.red);// 加上这句可以在Scene视窗看到射线

先写一下射线的应用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Global;
public class ShiYan : MonoBehaviour {
    Ray ray;
    RaycastHit hitInfoPoint;
 
    void Update () {
      
        if (Input .GetMouseButtonUp (0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            LayerMask layer = 1 << 10;     //层级 就是打开第10层
            //下面如果不判断,并且你的场景中没有其他的碰撞体,就会报错,为了防止报错加个判断if
            if (Physics.Raycast(ray, out hitInfoPoint, 100f, layer))
            {
                Debug.Log(hitInfoPoint.transform.name);
            }
                                                           
        }     
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章