-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRotateTowardsObject.cs
74 lines (68 loc) · 2.99 KB
/
RotateTowardsObject.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
using UnityEngine;
namespace BehaviorDesigner.Runtime.Tasks.Movement
{
[TaskDescription("Rotates towards the specified rotation. The rotation can either be specified by a transform or rotation. If the transform " +
"is used then the rotation will not be used.")]
[TaskCategory("Movement")]
[HelpURL("http://www.opsive.com/assets/BehaviorDesigner/Movement/documentation.php?id=2")]
[TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}RotateTowardsIcon.png")]
public class RotateTowardsObject : Action
{
public SharedGameObject objectToRotate;
[Tooltip("Should the 2D version be used?")]
public bool usePhysics2D;
[Tooltip("The agent is done rotating when the angle is less than this value")]
public SharedFloat rotationEpsilon = 0.5f;
[Tooltip("The maximum number of angles the agent can rotate in a single tick")]
public SharedFloat maxLookAtRotationDelta = 1;
[Tooltip("Should the rotation only affect the Y axis?")]
public SharedBool onlyY;
[Tooltip("The GameObject that the agent is rotating towards")]
public SharedGameObject target;
[Tooltip("If target is null then use the target rotation")]
public SharedVector3 targetRotation;
Transform objecTrans;
public override TaskStatus OnUpdate()
{
objecTrans = objectToRotate.Value.gameObject.transform;
var rotation = Target();
// Return a task status of success once we are done rotating
if (Quaternion.Angle(objecTrans.rotation, rotation) < rotationEpsilon.Value)
{
return TaskStatus.Success;
}
// We haven't reached the target yet so keep rotating towards it
objecTrans.rotation = Quaternion.RotateTowards(objecTrans.rotation, rotation, maxLookAtRotationDelta.Value);
return TaskStatus.Running;
}
// Return targetPosition if targetTransform is null
private Quaternion Target()
{
if (target == null || target.Value == null)
{
return Quaternion.Euler(targetRotation.Value);
}
var position = target.Value.transform.position - objecTrans.position;
if (onlyY.Value)
{
position.y = 0;
}
if (usePhysics2D)
{
var angle = Mathf.Atan2(position.y, position.x) * Mathf.Rad2Deg;
return Quaternion.AngleAxis(angle, Vector3.forward);
}
return Quaternion.LookRotation(position);
}
// Reset the public variables
public override void OnReset()
{
usePhysics2D = false;
rotationEpsilon = 0.5f;
maxLookAtRotationDelta = 1f;
onlyY = false;
target = null;
targetRotation = Vector3.zero;
}
}
}