-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathVisionCone.cs
72 lines (63 loc) · 2.61 KB
/
VisionCone.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class VisionCone : MonoBehaviour
{
public Material VisionConeMaterial;
public float VisionRange;
public float VisionAngle;
public LayerMask VisionObstructingLayer;//layer with objects that obstruct the enemy view, like walls, for example
public int VisionConeResolution = 120;//the vision cone will be made up of triangles, the higher this value is the pretier the vision cone will be
Mesh VisionConeMesh;
MeshFilter MeshFilter_;
//Create all of these variables, most of them are self explanatory, but for the ones that aren't i've added a comment to clue you in on what they do
//for the ones that you dont understand dont worry, just follow along
void Start()
{
transform.AddComponent<MeshRenderer>().material = VisionConeMaterial;
MeshFilter_ = transform.AddComponent<MeshFilter>();
VisionConeMesh = new Mesh();
VisionAngle *= Mathf.Deg2Rad;
}
void Update()
{
DrawVisionCone();//calling the vision cone function everyframe just so the cone is updated every frame
}
void DrawVisionCone()//this method creates the vision cone mesh
{
int[] triangles = new int[(VisionConeResolution - 1) * 3];
Vector3[] Vertices = new Vector3[VisionConeResolution + 1];
Vertices[0] = Vector3.zero;
float Currentangle = -VisionAngle / 2;
float angleIcrement = VisionAngle / (VisionConeResolution - 1);
float Sine;
float Cosine;
for (int i = 0; i < VisionConeResolution; i++)
{
Sine = Mathf.Sin(Currentangle);
Cosine = Mathf.Cos(Currentangle);
Vector3 RaycastDirection = (transform.forward * Cosine) + (transform.right * Sine);
Vector3 VertForward = (Vector3.forward * Cosine) + (Vector3.right * Sine);
if (Physics.Raycast(transform.position, RaycastDirection, out RaycastHit hit, VisionRange, VisionObstructingLayer))
{
Vertices[i + 1] = VertForward * hit.distance;
}
else
{
Vertices[i + 1] = VertForward * VisionRange;
}
Currentangle += angleIcrement;
}
for (int i = 0, j = 0; i < triangles.Length; i += 3, j++)
{
triangles[i] = 0;
triangles[i + 1] = j + 1;
triangles[i + 2] = j + 2;
}
VisionConeMesh.Clear();
VisionConeMesh.vertices = Vertices;
VisionConeMesh.triangles = triangles;
MeshFilter_.mesh = VisionConeMesh;
}
}