diff --git a/Assets/MGS-PathAnimation/Editor/PathAnimationEditor.cs b/Assets/MGS-PathAnimation/Editor/PathAnimationEditor.cs index f1bb2a5..38f1d27 100644 --- a/Assets/MGS-PathAnimation/Editor/PathAnimationEditor.cs +++ b/Assets/MGS-PathAnimation/Editor/PathAnimationEditor.cs @@ -25,9 +25,15 @@ public class PathAnimationEditor : Editor { #region Property and Field protected PathAnimation script { get { return target as PathAnimation; } } + protected SerializedProperty reference; #endregion #region Protected Method + protected virtual void OnEnable() + { + reference = serializedObject.FindProperty("reference"); + } + protected void MarkSceneDirty() { #if UNITY_5_3_OR_NEWER @@ -43,6 +49,12 @@ public override void OnInspectorGUI() { DrawDefaultInspector(); + if (script.keepUpMode == KeepUpMode.ReferenceForward || script.keepUpMode == KeepUpMode.ReferenceForwardAsNormal) + { + EditorGUILayout.PropertyField(reference); + serializedObject.ApplyModifiedProperties(); + } + if (GUILayout.Button("AlignToPath")) { script.AlignToPath(); diff --git a/Assets/MGS-PathAnimation/Scripts/PathAnimation.cs b/Assets/MGS-PathAnimation/Scripts/PathAnimation.cs index f291563..b83050d 100644 --- a/Assets/MGS-PathAnimation/Scripts/PathAnimation.cs +++ b/Assets/MGS-PathAnimation/Scripts/PathAnimation.cs @@ -14,6 +14,14 @@ namespace Developer.PathAnimation { + public enum KeepUpMode + { + WorldUp = 0, + TransformUp = 1, + ReferenceForward = 2, + ReferenceForwardAsNormal = 3 + } + [AddComponentMenu("Developer/PathAnimation/PathAnimation")] public class PathAnimation : MonoBehaviour { @@ -34,6 +42,17 @@ public class PathAnimation : MonoBehaviour [SerializeField] protected WrapMode wrapMode = WrapMode.Default; + /// + /// Keep up mode on play animation. + /// + public KeepUpMode keepUpMode = KeepUpMode.WorldUp; + + /// + /// Keep up reference transform. + /// + [HideInInspector] + public Transform reference; + /// /// Timer of animation. /// @@ -63,12 +82,33 @@ protected virtual void Update() /// Time of path curve. protected void TowTransformBaseOnPath(float time) { - var pathPoint = path.GetPointOnCurve(time); - var tangent = (path.GetPointOnCurve(time + delta) - pathPoint).normalized; + var timePos = path.GetPointOnCurve(time); + var deltaPos = path.GetPointOnCurve(time + delta); + + var worldUp = Vector3.up; + switch (keepUpMode) + { + case KeepUpMode.TransformUp: + worldUp = transform.up; + break; + + case KeepUpMode.ReferenceForward: + if (reference) + worldUp = reference.forward; + break; + + case KeepUpMode.ReferenceForwardAsNormal: + if (reference) + { + var secant = (deltaPos - timePos).normalized; + worldUp = Vector3.Cross(secant, reference.forward); + } + break; + } //Update position and look at tangent. - transform.position = pathPoint; - transform.LookAt(pathPoint + tangent, Vector3.up); + transform.position = timePos; + transform.LookAt(deltaPos, worldUp); } #endregion