-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaypointDirection.cs
63 lines (51 loc) · 1.91 KB
/
WaypointDirection.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
using UnityEngine;
namespace app.item.junction
{
public class WaypointDirection : MonoBehaviour
{
public enum Direction
{
none,
Forward,
Left,
Right,
Backward
}
public Direction Location(GameObject playerGameObject)
{
Direction waypointDirection = Direction.none;
Vector3 targetDir = transform.position - playerGameObject.transform.position;
targetDir = targetDir.normalized;
float dot = Vector3.Dot(targetDir, playerGameObject.transform.forward);
dot = Mathf.Clamp(dot, -1f, 1f); // Clamping dot because number can be slightly outside -1 to 1 and breaks the angle
float angle = Mathf.Acos(dot) * Mathf.Rad2Deg; // The angle is from 0 to 180. Vector3.Cross is used below to determine positive and negative (left/right)
Vector3 normalizedDirection = transform.position - playerGameObject.transform.position;
float direction = Vector3.Cross(playerGameObject.transform.forward, normalizedDirection).y;
//Debug.Log("Angle: " + angle + " Dir: " + direction);
if (angle <= 45)
{
//Debug.Log("Front");
waypointDirection = Direction.Forward;
}
if (angle >= 135)
{
//Debug.Log("Behind");
waypointDirection = Direction.Backward;
}
if (angle > 45 && angle < 135)
{
if (direction >= 0)
{
//Debug.Log("Right");
waypointDirection = Direction.Right;
}
else
{
//Debug.Log("Left");
waypointDirection = Direction.Left;
}
}
return waypointDirection;
}
}
}