起点 终点检测射线 进阶射线
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);
}
}
}
}