diff --git a/Assets/PathTools/Scenes/PathToolsExample.unity b/Assets/PathTools/Scenes/PathToolsExample.unity index 1879c0a..dfe1563 100644 --- a/Assets/PathTools/Scenes/PathToolsExample.unity +++ b/Assets/PathTools/Scenes/PathToolsExample.unity @@ -170,9 +170,10 @@ MonoBehaviour: tangentType: 0 selectedId: 0 closeLoop: 1 - lastPos: {x: -0.2224189, y: 0.013729572, z: 2.9288592} - lastLeftHandlePos: {x: 2.0906916, y: 0.013729572, z: 3.5246074} - lastRightHandlePos: {x: -2.2515397, y: 0.013729572, z: 2.4062533} + showUpVector: 0 + lastPos: {x: 1.4542342, y: 0.013729572, z: 0.3277291} + lastLeftHandlePos: {x: 1.237987, y: 0.013729572, z: -0.6486104} + lastRightHandlePos: {x: 1.6652508, y: 0.013729572, z: 1.2804526} --- !u!4 &800040797 Transform: m_ObjectHideFlags: 0 @@ -305,6 +306,8 @@ MonoBehaviour: speed: 2 rotationSpeed: 5 loopMode: 0 + useCustomUpVector: 0 + customUpVector: {x: 0, y: 1, z: 0} distance: 0 --- !u!23 &1977854513 MeshRenderer: diff --git a/Assets/PathTools/Scripts/Editor/NodeMoveEditor.cs b/Assets/PathTools/Scripts/Editor/NodeMoveEditor.cs deleted file mode 100644 index 93182d2..0000000 --- a/Assets/PathTools/Scripts/Editor/NodeMoveEditor.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEditor; -using UnityEditor.SceneManagement; - -[CustomEditor(typeof(NodeMove))] -public class NodeMoveEditor : Editor -{ - NodeMove source; - - public override void OnInspectorGUI() - { - source = (NodeMove)target; - - base.OnInspectorGUI(); - - if (GUILayout.Button("Add Nodes!")) - { - source.nodes.Add(source.transform.position); - } - - if (GUILayout.Button("Remove Nodes!")) - { - source.nodes.RemoveAt(source.nodes.Count - 1); - } - - if (GUI.changed) - { - EditorUtility.SetDirty(source); - EditorSceneManager.MarkSceneDirty(source.gameObject.scene); - } - } - - private void OnSceneGUI() - { - source = (NodeMove)target; - - for (int i = 0; i < source.nodes.Count; i++) - { - source.nodes[i] = source.transform.InverseTransformPoint(Handles.PositionHandle(source.transform.TransformPoint(source.nodes[i]), Quaternion.identity)); - Handles.Label(source.transform.TransformPoint(source.nodes[i]), "Nodes" + (i + 1)); - } - - if (GUI.changed) - { - EditorUtility.SetDirty(source); - EditorSceneManager.MarkSceneDirty(source.gameObject.scene); - } - } -} diff --git a/Assets/PathTools/Scripts/Editor/NodeMoveEditor.cs.meta b/Assets/PathTools/Scripts/Editor/NodeMoveEditor.cs.meta deleted file mode 100644 index 2ecd0d4..0000000 --- a/Assets/PathTools/Scripts/Editor/NodeMoveEditor.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 4a102efc46d04a6428adeca1b4e065b1 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/PathTools/Scripts/Editor/PathScriptEditor.cs b/Assets/PathTools/Scripts/Editor/PathScriptEditor.cs index e810f53..0cc7776 100644 --- a/Assets/PathTools/Scripts/Editor/PathScriptEditor.cs +++ b/Assets/PathTools/Scripts/Editor/PathScriptEditor.cs @@ -59,6 +59,7 @@ public override void OnInspectorGUI() source.AddNode(); source.closeLoop = EditorGUILayout.Toggle("Close Loop", source.closeLoop); + source.showUpVector = EditorGUILayout.Toggle("Show Orientation", source.showUpVector); EditorGUILayout.LabelField(string.Format("Path Length: {0}", source.PathDistance)); if (EditorGUI.EndChangeCheck()) diff --git a/Assets/PathTools/Scripts/MoveAlongPath.cs b/Assets/PathTools/Scripts/MoveAlongPath.cs index f4179be..1ebbfd6 100644 --- a/Assets/PathTools/Scripts/MoveAlongPath.cs +++ b/Assets/PathTools/Scripts/MoveAlongPath.cs @@ -10,6 +10,10 @@ public class MoveAlongPath : MonoBehaviour [SerializeField] float speed = 2f, rotationSpeed = 5f; [SerializeField] LoopMode loopMode; + [Space(20)] + [SerializeField] bool useCustomUpVector; + [SerializeField] Vector3 customUpVector = Vector3.up; + [Header("Debug")] [SerializeField] float distance; @@ -43,7 +47,7 @@ void Update() } transform.position = path.GetPositionAtDistance(runtimeDistance); - Quaternion targetRot = path.GetRotationAtDistance(runtimeDistance, path.GetUpVectorAtDistance(runtimeDistance)); + Quaternion targetRot = path.GetRotationAtDistance(runtimeDistance, useCustomUpVector ? customUpVector : path.GetUpVectorAtDistance(runtimeDistance)); transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, rotationSpeed * Time.deltaTime); } diff --git a/Assets/PathTools/Scripts/NodeMove.cs b/Assets/PathTools/Scripts/NodeMove.cs deleted file mode 100644 index bd4e7d2..0000000 --- a/Assets/PathTools/Scripts/NodeMove.cs +++ /dev/null @@ -1,174 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class NodeMove : MonoBehaviour -{ - public float speed = 2f, rotateSpeed = 2f, bankingValue = 5f; - public bool rotateObject, loopMove; - public int loopToNode; - public List nodes = new List(); - - [SerializeField] bool baked; - - private const int CURVE_SEGMENT = 20; - private int realLoopNode; - private Transform parent; - private float nextAngleGrab; - - private List path = new List(); - - private Vector3 startPos; - - private Quaternion rotation; - - private void OnEnable() - { - path = GetCurveNodes(); - - startPos = transform.position; - - DefineParent(); - - StartCoroutine(StartMove()); - } - - private void OnDisable() - { - StopAllCoroutines(); - } - - IEnumerator StartMove() - { - float oldAngle = 0f; - int posID = 1; - - transform.position = startPos + path[0]; - - while (loopMove || posID < path.Count - 1) - { - //this handles the index of the path vectors, and decide the next target position - if (((startPos + path[posID]) - transform.position).sqrMagnitude < 0.01f) - { - if (loopMove) - { - if (posID < path.Count - 1) - { - posID += 1; - } - else - { - posID = realLoopNode; - } - } - else - { - if (posID < path.Count - 1) - { - posID += 1; - } - } - } - - transform.position = Vector3.MoveTowards(transform.position, startPos + path[posID], speed * Time.deltaTime); - - if (rotateObject) - { - if (Time.time > nextAngleGrab) - { - nextAngleGrab = Time.time + 0.5f; - - Vector3 dir = (startPos + path[posID]) - transform.position; - - if (dir.sqrMagnitude > 0.01f) - { - rotation = Quaternion.LookRotation(dir, Vector3.up); - } - - float zBank = Mathf.Clamp(rotation.eulerAngles.y - oldAngle, -10f, 10f); - - Quaternion banking = Quaternion.Euler(0f, 0f, Mathf.Ceil(zBank) * -bankingValue); - - rotation *= banking; - - oldAngle = rotation.eulerAngles.y; - } - - transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotateSpeed * Time.deltaTime); - } - - yield return null; - } - } - - List GetCurveNodes() - { - DefineParent(); - - List curvedNodes = new List(); - - for (int i = 0; i < nodes.Count - 3; i += 3) - { - Vector3 p0 = (nodes[i]); - Vector3 p1 = (nodes[i + 1]); - Vector3 p2 = (nodes[i + 2]); - Vector3 p3 = (nodes[i + 3]); - - for (int j = 0; j <= CURVE_SEGMENT; j++) - { - float t = j / (float)CURVE_SEGMENT; - curvedNodes.Add(CalculateBezierPath(p0, p1, p2, p3, t)); - } - } - - realLoopNode = (int)(curvedNodes.Count * (loopToNode / (float)nodes.Count)); - - return curvedNodes; - } - - private void DefineParent() - { - if (transform.parent) - { - parent = transform.parent; - } - else - { - parent = transform; - } - } - - Vector3 CalculateBezierPath(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) - { - // (1 - t)^3 * p0 + 3(1-t)^2 * t * p1 + 3(1-t) * t^2 * p2 + t^3 * p3 - - float oneMinusT = 1f - t; - - Vector3 result = Mathf.Pow(oneMinusT, 3f) * p0 + 3f * Mathf.Pow(oneMinusT, 2f) * t * p1 - + 3f * oneMinusT * (t * t) * p2 + Mathf.Pow(t, 3f) * p3; - - return result; - } - - private void OnDrawGizmosSelected() - { - DefineParent(); - - Vector3 offset = Application.isPlaying ? startPos : transform.position; - List curvePositions = Application.isPlaying ? path : GetCurveNodes(); - - for (int i = 1; i < curvePositions.Count; i++) - { - Gizmos.color = Color.green; - Gizmos.DrawLine(offset + curvePositions[i - 1], offset + curvePositions[i]); - } - - for (int i = 1; i < nodes.Count; i++) - { - Color gizmoColor = Color.yellow; - gizmoColor.a = 0.5f; - Gizmos.color = gizmoColor; - Gizmos.DrawLine(offset + (nodes[i - 1]), offset + (nodes[i])); - } - } -} diff --git a/Assets/PathTools/Scripts/NodeMove.cs.meta b/Assets/PathTools/Scripts/NodeMove.cs.meta deleted file mode 100644 index 459fd62..0000000 --- a/Assets/PathTools/Scripts/NodeMove.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 710206cfd29fc5241b485da4d65a54bf -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/PathTools/Scripts/PathScript.cs b/Assets/PathTools/Scripts/PathScript.cs index 2e1970d..c819cc2 100644 --- a/Assets/PathTools/Scripts/PathScript.cs +++ b/Assets/PathTools/Scripts/PathScript.cs @@ -9,7 +9,7 @@ public class PathScript : MonoBehaviour #region VARIABLES [SerializeField] private List nodes = new List(); [SerializeField] private int selectedId; - public bool closeLoop; + public bool closeLoop, showUpVector; private const int CURVE_SEGMENT = 20; @@ -236,13 +236,16 @@ private void OnDrawGizmos() Gizmos.DrawLine(LocalToWorld(curvedPositions[i == 0 ? curvedPositions.Count - 1 : i - 1]), LocalToWorld(curvedPositions[i])); } - for (int i = 0; i < orientations.Count; i++) + if (showUpVector) { - //draw point up with orientation influence - Vector3 direction = (curvedPositions[i] - curvedPositions[i == 0 ? (curvedPositions.Count - 1) : (i - 1)]).normalized; - Vector3 finalDirection = Quaternion.AngleAxis(orientations[i], direction) * Vector3.up; - Gizmos.color = Color.red; - Gizmos.DrawLine(LocalToWorld(curvedPositions[i]), LocalToWorld(curvedPositions[i]) + (finalDirection * 0.4f)); + for (int i = 0; i < orientations.Count; i++) + { + //draw point up with orientation influence + Vector3 direction = (curvedPositions[i] - curvedPositions[i == 0 ? (curvedPositions.Count - 1) : (i - 1)]).normalized; + Vector3 finalDirection = Quaternion.AngleAxis(orientations[i], direction) * Vector3.up; + Gizmos.color = Color.red; + Gizmos.DrawLine(LocalToWorld(curvedPositions[i]), LocalToWorld(curvedPositions[i]) + (finalDirection * 0.4f)); + } } } #endif