-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSmoothFollowActionNoLookAt.cs
102 lines (80 loc) · 3.18 KB
/
SmoothFollowActionNoLookAt.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
94
95
96
97
98
99
100
101
102
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Action version of Unity's Smooth Follow script. Does not look at the target so that it allows you to use a separate look at action.")]
public class SmoothFollowActionNoLookAt : FsmStateAction
{
[RequiredField]
[Tooltip("The game object to control. E.g. The camera.")]
public FsmOwnerDefault gameObject;
[Tooltip("The GameObject to follow usually the Player.")]
public FsmGameObject targetObject;
// [Tooltip("The position to look at.")]
// public FsmVector3 lookTarget;
[RequiredField]
[Tooltip("The distance in the x-z plane to the target.")]
public FsmFloat distance;
[RequiredField]
[Tooltip("The height we want the camera to be above the target")]
public FsmFloat height;
[RequiredField]
[Tooltip("How much to dampen height movement. More = tighter following.")]
public FsmFloat heightDamping;
[RequiredField]
[Tooltip("How much to dampen rotation changes. More = less choppiness.")]
public FsmFloat rotationDamping;
// Cache for performance
private GameObject cachedObect;
private Transform myTransform;
private Transform targetTransform;
public override void Reset()
{
gameObject = null;
targetObject = null;
distance = 10f;
height = 5f;
heightDamping = 2f;
rotationDamping = 3f;
// lookTarget = null;
}
public override void OnLateUpdate()
{
if (targetObject.Value == null)
{
return;
}
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}
if (cachedObect != go)
{
cachedObect = go;
myTransform = go.transform;
targetTransform = targetObject.Value.transform;
}
// Calculate the current rotation angles
//var wantedRotationAngle = targetTransform.eulerAngles.y;
var wantedHeight = targetTransform.position.y + height.Value;
var currentRotationAngle = myTransform.eulerAngles.y;
var currentHeight = myTransform.position.y;
// Damp the rotation around the y-axis
// currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping.Value * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping.Value * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
//// Set the position of the camera on the x-z plane to:
//// distance meters behind the target
myTransform.position = targetTransform.position;
myTransform.position -= currentRotation * Vector3.forward * distance.Value;
// Set the height of the camera
myTransform.position = new Vector3(myTransform.position.x, currentHeight, myTransform.position.z);
//// Always look at the target
// myTransform.LookAt(lookTarget.Value);
}
}
}