forked from mdotstrange/CustomBehaviorDesignerTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsGameObjectFrontOrBehind.cs
53 lines (40 loc) · 1.57 KB
/
IsGameObjectFrontOrBehind.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
//Important code is from http://www.habrador.com/tutorials/linear-algebra/1-behind-or-in-front/
using UnityEngine;
namespace BehaviorDesigner.Runtime.Tasks.Basic.UnityGameObject
{
[TaskCategory("Basic/GameObject")]
[TaskDescription("Determines whether a game object is in front of or behind another. Always returns success. Stores results in bools")]
public class IsGameObjectFrontOrBehind : Action
{
public SharedGameObject baseGameObject;
public SharedGameObject targetGameObject;
public SharedBool inFront;
public SharedBool Behind;
public override TaskStatus OnUpdate()
{
Vector3 youForward = baseGameObject.Value.gameObject.transform.forward;
Vector3 youToEnemy = targetGameObject.Value.gameObject.transform.position - baseGameObject.Value.gameObject.transform.position;
float dotProduct = DotProduct(youForward, youToEnemy);
if (dotProduct >= 0f)
{
Behind.Value = false;
inFront = true;
return TaskStatus.Success;
} else
{
Behind.Value = true;
inFront = false;
return TaskStatus.Success;
}
}
float DotProduct(Vector3 vec1, Vector3 vec2)
{
float dotProduct = vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
return dotProduct;
}
public override void OnReset()
{
}
}
}