-
Notifications
You must be signed in to change notification settings - Fork 4
/
EnemySightSensor.cs
55 lines (42 loc) · 1.38 KB
/
EnemySightSensor.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Demo.Enemy
{
public class EnemySightSensor : MonoBehaviour
{
public Transform Player { get; private set; }
[SerializeField] private LayerMask _ignoreMask;
private Ray _ray;
private void Awake()
{
Player = GameObject.Find("Player").transform;
}
public bool Ping()
{
if (Player == null)
return false;
_ray = new Ray(this.transform.position, Player.position-this.transform.position);
var dir = new Vector3(_ray.direction.x, 0, _ray.direction.z);
var angle = Vector3.Angle(dir, this.transform.forward);
if (angle>60)
return false;
if (!Physics.Raycast(_ray, out var hit, 100, ~_ignoreMask))
{
return false;
}
if(hit.collider.tag == "Player")
{
return true;
}
return false;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(_ray.origin, _ray.origin + _ray.direction * 100);
Gizmos.color = Color.blue;
Gizmos.DrawLine(this.transform.position, this.transform.position + this.transform.forward * 100);
}
}
}