射线记录

起点  终点检测射线   进阶射线

public int raycastCount = 10;
	
	private bool started = false;
	private Vector3 start, end;
	
	public void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			start = Input.mousePosition;
			
			started = true;
		}
		
		if (Input.GetMouseButtonUp(0) && started)
		{
			end = Input.mousePosition;
			
			// Calculate the world-space line
			Camera mainCamera = Camera.main;
			
			float near = mainCamera.nearClipPlane;
			
			Vector3 line = mainCamera.ScreenToWorldPoint(new Vector3(end.x, end.y, near)) - mainCamera.ScreenToWorldPoint(new Vector3(start.x, start.y, near));
			
			// Find game objects to split by raycasting at points along the line
			for (int i = 0; i < raycastCount; i++)
			{
				Ray ray = mainCamera.ScreenPointToRay(Vector3.Lerp(start, end, (float)i / raycastCount));

                RaycastHit[] hit = Physics.RaycastAll(ray, Mathf.Infinity, 1 << LayerMask.NameToLayer("pao"));

                for (int j = 0; j < hit.Length; j++)
                {
                    Plane splitPlane = new Plane(Vector3.Normalize(Vector3.Cross(line, ray.direction)), hit[j].point);

                    hit[j].collider.SendMessage("Split", new Plane[] { splitPlane }, SendMessageOptions.DontRequireReceiver);
                }

              
			}
			
			started = false;
		}
	}
if (Physics.Raycast(vectorsPos[i], Vector3.down, out RaycastHit hit, 1000))
                {

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山竹炒大蒜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值