forked from mdotstrange/CustomBehaviorDesignerTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveGameObjectTowards.cs
93 lines (77 loc) · 2.61 KB
/
MoveGameObjectTowards.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Converted Playmaker action "Move Towards"
using UnityEngine;
namespace BehaviorDesigner.Runtime.Tasks.Basic.UnityGameObject
{
[TaskCategory("Basic/GameObject")]
[TaskDescription("Move a game object to another game object's position. Return success when it reaches the finish distance.")]
public class MoveGameObjectTowards : Action
{
public SharedGameObject ObjectToMove;
public SharedGameObject targetObject;
public SharedVector3 targetPosition;
public SharedBool ignoreVertical;
public SharedFloat maxSpeed;
public SharedFloat finishDistance;
private GameObject go;
private GameObject goTarget;
private Vector3 targetPos;
private Vector3 targetPosWithVertical;
public override TaskStatus OnUpdate()
{
if (!UpdateTargetPos())
{
return TaskStatus.Failure;
}
go.transform.position = Vector3.MoveTowards(go.transform.position, targetPos, maxSpeed.Value * Time.deltaTime);
var distance = (go.transform.position - targetPos).magnitude;
if (distance < finishDistance.Value)
{
return TaskStatus.Success;
}
return TaskStatus.Running;
}
public bool UpdateTargetPos()
{
go = ObjectToMove.Value;
if (go == null)
{
return false;
}
goTarget = targetObject.Value;
if (goTarget == null && targetPosition.IsNone)
{
return false;
}
if (goTarget != null)
{
targetPos = !targetPosition.IsNone ?
goTarget.transform.TransformPoint(targetPosition.Value) :
goTarget.transform.position;
} else
{
targetPos = targetPosition.Value;
}
targetPosWithVertical = targetPos;
if (ignoreVertical.Value)
{
targetPos.y = go.transform.position.y;
}
return true;
}
public Vector3 GetTargetPos()
{
return targetPos;
}
public Vector3 GetTargetPosWithVertical()
{
return targetPosWithVertical;
}
public override void OnReset()
{
gameObject = null;
targetObject = null;
maxSpeed = 10f;
finishDistance = 1f;
}
}
}