Skip to content

Commit

Permalink
Optimize PathAnimation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mogoson committed Nov 19, 2017
1 parent f726b38 commit d5bbc9b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
12 changes: 12 additions & 0 deletions Assets/MGS-PathAnimation/Editor/PathAnimationEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
48 changes: 44 additions & 4 deletions Assets/MGS-PathAnimation/Scripts/PathAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -34,6 +42,17 @@ public class PathAnimation : MonoBehaviour
[SerializeField]
protected WrapMode wrapMode = WrapMode.Default;

/// <summary>
/// Keep up mode on play animation.
/// </summary>
public KeepUpMode keepUpMode = KeepUpMode.WorldUp;

/// <summary>
/// Keep up reference transform.
/// </summary>
[HideInInspector]
public Transform reference;

/// <summary>
/// Timer of animation.
/// </summary>
Expand Down Expand Up @@ -63,12 +82,33 @@ protected virtual void Update()
/// <param name="time">Time of path curve.</param>
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

Expand Down

0 comments on commit d5bbc9b

Please sign in to comment.