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