效果图如下:
大扇形和小扇形代码如下:
using UnityEngine;
public class HuaShanXing : MonoBehaviour
{
public int jiaodu = 120; //角度
public int banjing = 120; //半径
public int banjing2 = 100; //半径
private Transform head;//射线出发点
private LineRenderer lineRenderer;
private LineRenderer lineRenderer2;
void Start()
{
head = transform.Find("Head");
DrawShanXing2();
DrawShanXing();
}
public void DrawShanXing()
{
GameObject go = new GameObject();
go.transform.tag = "ShanXing";
go.transform.parent = head.parent;
// 设置线条上的顶点数量
lineRenderer = go.AddComponent<LineRenderer>();
Material sourceMat = Resources.Load<Material>("MyMaterials/ShanXingMatGreen"); //新建材质;
if (sourceMat != null)
{
Material mat = new Material(sourceMat); //新建材质;
//Debug.Log(mat.name);
mat.shader = Shader.Find("Unlit/Color");//设置材质的shader类型
mat.color = new Color(0.5f, 1f, 0f, 1f);//为材质的颜色赋值(rgba通道)
lineRenderer.material = mat;//将创建的材质赋值给线渲染组件
}
// 设置线条宽度
lineRenderer.startWidth = 1f;
lineRenderer.endWidth = 1f;
//绘制扇形
int a = 1;
lineRenderer.loop = true;
lineRenderer.positionCount = jiaodu + 2;
lineRenderer.SetPosition(0, head.transform.position+new Vector3(0,-0.1f,0));
lineRenderer.SetPosition(jiaodu + 1, head.transform.position + new Vector3(0, -0.1f, 0));
for (int i = -jiaodu / 2; i < jiaodu / 2; i++)
{
lineRenderer.SetPosition(a, Quaternion.Euler(0, i, 0) * head.transform.forward * banjing + head.transform.position + new Vector3(0, -0.1f, 0));
a++;
}
}
public void DrawShanXing2()
{
GameObject go = new GameObject();
go.transform.tag = "ShanXing";
go.transform.parent = head.parent;
// 设置线条上的顶点数量
lineRenderer2 = go.AddComponent<LineRenderer>();
Material mat2 = Resources.Load<Material>("MyMaterials/ShanXingMatRed"); //新建材质;
mat2.shader = Shader.Find("Unlit/Color");//设置材质的shader类型
mat2.color = new Color(1f, 0f, 0f, 1f);//为材质的颜色赋值(rgba通道)
lineRenderer2.material = mat2;//将创建的材质赋值给线渲染组件
// 设置线条宽度
lineRenderer2.startWidth = 1f;
lineRenderer2.endWidth = 1f;
int a = 1;
lineRenderer2.loop = true;
lineRenderer2.positionCount = jiaodu + 2;
lineRenderer2.SetPosition(0, head.transform.position);
lineRenderer2.SetPosition(jiaodu + 1, head.transform.position);
for (int i = -jiaodu / 2; i < jiaodu / 2; i++)
{
lineRenderer2.SetPosition(a, Quaternion.Euler(0, i, 0) * head.transform.forward * banjing2 + head.transform.position);
a++;
}
}
public void ResetDrawShanXing()
{
// if (LeftViewshedCtrol.instance.gameObjectPepleFather.transform.childCount > 0)
lineRenderer.positionCount = 0;
lineRenderer2.positionCount = 0;
for (int i = 1; i < head.parent.childCount; i++)
{
Transform shanxing = head.parent.transform.GetChild(i);
if (shanxing.tag == "ShanXing")
{
Destroy(shanxing.gameObject);
}
}
}
}
下面代码写的是红色区域射线检测部分:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddViewPoint : MonoBehaviour
{
public static AddViewPoint instance;
public GameObject gameObjectFov;
private Transform gameObjectFovParent;
private GameObject gameObjectHead;
public float viewRadius = 5f;
public int viewAngleStep = 20;
[Range(0, 360)]
private float viewAngle = 120f;
private Vector3 pos;
private void Awake()
{
instance = this;
}
void Start()
{
gameObjectFovParent = transform.Find("Head");
gameObjectHead = transform.Find("Head").gameObject;
// DrawFieldOfView();
}
void Update()
{
DrawFieldOfView();
}
public void ResetFieldOfView()
{
if (LeftViewshedCtrol.instance.gameObjectPepleFather.transform.childCount > 0)
{
pos = new Vector3(0, 0, 0);
foreach (Transform t in gameObjectFovParent)
{
Destroy(t.gameObject);
}
}
}
public void DrawFieldOfView()
{
if (LeftViewshedCtrol.instance.gameObjectPepleFather.transform.childCount > 0)
{
Vector3 forward_left = Quaternion.Euler(0, -(viewAngle / 2f), 0) * gameObjectHead.transform.forward * viewRadius;
for (int i = 0; i <= viewAngleStep; i++)
{
Vector3 v = Quaternion.Euler(0, (viewAngle / viewAngleStep) * i, 0) * forward_left;
pos = gameObjectHead.transform.position + v;
//Debug.DrawLine(gameObjectHead.transform.position, pos, Color.red);
Ray ray = new Ray(gameObjectHead.transform.position, v);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, viewRadius))
{
if (hitInfo.collider.tag == "Building")
{
GameObject go = Instantiate(gameObjectFov);
go.transform.parent = gameObjectFovParent;
go.transform.position = hitInfo.point + new Vector3(0, 0, 0);
LineRenderer lineRenderer = go.AddComponent<LineRenderer>();
Material mat2 = Resources.Load<Material>("MyMaterials/ShanXingMatRed");
mat2.shader = Shader.Find("Unlit/Color");
mat2.color = new Color(1f, 0f, 0f, 1f);
lineRenderer.material = mat2;
lineRenderer.startWidth = 0.4f;
lineRenderer.endWidth = 0.4f;
lineRenderer.SetPosition(0, gameObjectHead.transform.position);
lineRenderer.SetPosition(1, go.transform.position);
}
}
else
{
GameObject go2 = Instantiate(gameObjectFov);
go2.transform.parent = gameObjectFovParent;
go2.transform.position = pos;
LineRenderer lineRenderer = go2.AddComponent<LineRenderer>();
Material mat2 = Resources.Load<Material>("MyMaterials/ShanXingMatRed");
mat2.shader = Shader.Find("Unlit/Color");
mat2.color = new Color(1f, 0f, 0f, 1f);
lineRenderer.material = mat2;
lineRenderer.startWidth = 0.4f;
lineRenderer.endWidth = 0.4f;
lineRenderer.SetPosition(0, gameObjectHead.transform.position);
lineRenderer.SetPosition(1, go2.transform.position);
}
}
}
}
}