-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cs
71 lines (58 loc) · 2.14 KB
/
Enemy.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
using System.Collections;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField]
[Range(1.0f, 5.0f)]
float rayDistanceForward = 5.0f;
[SerializeField]
[Range(1.0f, 5.0f)]
float rayDistanceSide = 3.0f;
[SerializeField]
[Range(1.0f, 5.0f)]
float rayDistanceBack = 1.0f;
RaycastHit hit;
int layerMaskExceptEnemy;
void Start()
{
// Bit shift the index of the layer (9 - Enemy) to get a bit mask. This would cast rays only against colliders in layer 9.
int enemyLayerMask = 1 << 9;
// But instead we want to collide against everything except layer 9. The ~ operator does this, it inverts a bitmask.
layerMaskExceptEnemy = ~enemyLayerMask;
StartCoroutine(RaycastTargetRoutine());
}
IEnumerator RaycastTargetRoutine()
{
float timeOffset = Time.time;
while (true)
{
// default init: x = 0 / z = 1
float frequency = 5.0f; // more than 5
float x = Mathf.Round(Mathf.Sin((Time.time - timeOffset) * frequency) * 10) / 10;
float z = Mathf.Round(Mathf.Sin((Time.time - timeOffset + 1.0f) * frequency) * 10) / 10;
Vector3 rayDirection = new Vector3(x, 0, z);
float distance;
float dotProduct = Vector3.Dot(transform.forward, rayDirection);
if (dotProduct >= Mathf.Epsilon)
{
distance = Mathf.SmoothStep(rayDistanceSide, rayDistanceForward, dotProduct);
}
else
{
distance = Mathf.SmoothStep(rayDistanceSide, rayDistanceBack,
-dotProduct); // invert because smoothstep runs with 0 to 1
}
RaycastTarget(rayDirection, distance);
yield return null;
}
}
void RaycastTarget(Vector3 rayDirection, float distance)
{
Ray ray = new Ray(transform.position, rayDirection);
Debug.DrawRay(ray.origin, ray.direction * distance, Color.yellow);
if (Physics.Raycast(ray, out hit, distance, layerMaskExceptEnemy))
{
// TODO: impl. the Player was seen
}
}
}