Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by the_genius · Nov 01, 2015 at 04:11 PM · scripting problemstandard-assetswaypoint systembuiltin array

Modifying Waypoint Circuit script

Hello,

In the unity vehicle standard assets there is a script for car ai to help create a waypoint circuit in the editor. All I am trying to do is turn it from a circuit to a curved line. (and I don't need it to work with the car ai scripts or anything else. I only need to change how it looks in the editor).

The script appears to work by turning an array of Transform points into a Vector3 array and it adds the start point to the end of the array to make it a loop.

So all I need to do is stop it from adding the start point to the end of the array to stop the two ends joining into a loop, which sounds simple, but it is confusing me.

Any help is appreciated. Thank you.

I think the bits which need changing are in the CachePositionsAndDistances or the DrawGizmos functions. Here is the script: (Its not very well commented which doesn't help)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 public class WaypointCircuit : MonoBehaviour {
 
     public WaypointList waypointList = new WaypointList();
     [SerializeField] bool smoothRoute = true;
     int numPoints;
     Vector3[] points;
     float[] distances;
 
     public float editorVisualisationSubsteps = 100;
     public float Length { get; private set; }
     public Transform[] Waypoints { get { return waypointList.items; } }
     
     //this being here will save GC allocs
     int p0n;
     int p1n;
     int p2n;
     int p3n;
 
     private float i;
     Vector3 P0;
     Vector3 P1;
     Vector3 P2;
     Vector3 P3;
 
     // Use this for initialization
     void Awake () {
         if (Waypoints.Length > 1)
         {
             CachePositionsAndDistances();
         }
         numPoints = Waypoints.Length;
     }
 
     public RoutePoint GetRoutePoint(float dist)
     {
         // position and direction
         Vector3 p1 = GetRoutePosition(dist);
         Vector3 p2 = GetRoutePosition(dist + 0.1f);
         Vector3 delta = p2-p1;
         return new RoutePoint( p1, delta.normalized );
     }
 
     public Vector3 GetRoutePosition(float dist)
     {
         int point = 0;
 
         if (Length == 0)
         {
             Length = distances[distances.Length-1];
         }
 
         dist = Mathf.Repeat(dist,Length);
 
         while (distances[point] < dist) { ++point; }
 
 
         // get nearest two points, ensuring points wrap-around start & end of circuit
         p1n = ((point-1) + numPoints) % numPoints;
         p2n = point;
         
         // found point numbers, now find interpolation value between the two middle points
 
         i = Mathf.InverseLerp(distances[p1n],distances[p2n],dist);
 
         if (smoothRoute)
         {
             // smooth catmull-rom calculation between the two relevant points
 
     
 
             // get indices for the surrounding 2 points, because
             // four points are required by the catmull-rom function
             p0n = ((point-2) + numPoints) % numPoints;
             p3n = (point+1) % numPoints;
 
             // 2nd point may have been the 'last' point - a dupe of the first,
             // (to give a value of max track distance instead of zero)
             // but now it must be wrapped back to zero if that was the case.
             p2n = p2n % numPoints;
 
             P0 = points[ p0n ];
             P1 = points[ p1n ];
             P2 = points[ p2n ];
             P3 = points[ p3n ];
 
             return CatmullRom(P0,P1,P2,P3,i);
 
         } else {
 
             // simple linear lerp between the two points:
 
             p1n = ((point-1) + numPoints) % numPoints;
             p2n = point;
 
             return Vector3.Lerp ( points[p1n], points[p2n], i );
 
         }
 
     }
 
 
 
     Vector3 CatmullRom(Vector3 _P0, Vector3 _P1, Vector3 _P2, Vector3 _P3, float _i)
     {
         // comments are no use here... it's the catmull-rom equation.
         // Un-magic this, lord vector!
         return 0.5f * ( (2 * _P1) + (-_P0 + _P2) * _i + (2*_P0 - 5*_P1 + 4*_P2 - _P3) * _i*_i + (-_P0 + 3*_P1 - 3*_P2 + _P3) * _i*_i*_i );
     }
 
 
     void CachePositionsAndDistances()
     {
         // transfer the position of each point and distances between points to arrays for
         // speed of lookup at runtime
         points = new Vector3[Waypoints.Length+1];
         distances = new float[Waypoints.Length+1];
 
         float accumulateDistance = 0;
         for (int i = 0; i<points.Length; ++i) {
             var t1 =  Waypoints[(i)% Waypoints.Length ];
             var t2 =  Waypoints[(i+1)% Waypoints.Length ];
             if (t1 != null && t2 != null)
             {
                 Vector3 p1 = t1.position;
                 Vector3 p2 = t2.position;
                 points[i] = Waypoints[i % Waypoints.Length].position;
                 distances[i] = accumulateDistance;
                 accumulateDistance += (p1 - p2).magnitude;
             } 
         }
     }
 
 
     void OnDrawGizmos()
     {
         DrawGizmos(false);
     }
 
     void OnDrawGizmosSelected()
     {
         DrawGizmos(true);
     }
 
     void DrawGizmos(bool selected)
     {
         waypointList.circuit = this;
         if (Waypoints.Length > 1)
         {
             numPoints = Waypoints.Length;
 
             CachePositionsAndDistances();
             Length = distances[distances.Length-1];
 
             Gizmos.color = selected ? Color.yellow : new Color(1,1,0,0.5f);
             Vector3 prev = Waypoints[0].position;
             if (smoothRoute)
             {
                 for (float dist=0; dist<Length; dist += Length/editorVisualisationSubsteps)
                 {
                     Vector3 next = GetRoutePosition(dist+1);
                     Gizmos.DrawLine( prev, next );
                     prev = next;
                 }
                 Gizmos.DrawLine( prev, Waypoints[0].position );
             } else {
 
                 for (int n=0; n<Waypoints.Length; ++n)
                 {
                     Vector3 next = Waypoints[(n+1) % Waypoints.Length].position;
                     Gizmos.DrawLine( prev, next );
                     prev = next;
                 }
             }
         }
     }
     
     [System.Serializable]
     public class WaypointList
     {
         public WaypointCircuit circuit;
         public Transform[] items = new Transform[0];
     }
 
     public struct RoutePoint
     {
         public Vector3 position;
         public Vector3 direction;
 
         public RoutePoint(Vector3 position, Vector3 direction)
         {
             this.position = position;
             this.direction = direction;
         }
 
     }
 
 
 }
 
 
 
 #if UNITY_EDITOR
 [CustomPropertyDrawer (typeof(WaypointCircuit.WaypointList))]
 public class WaypointListDrawer : PropertyDrawer
 {
     float lineHeight = 18;
     float spacing = 4;
     
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty (position, label, property);
         
         float x = position.x;
         float y = position.y;
         float inspectorWidth = position.width;
         
         // Draw label
         
         
         // Don't make child fields be indented
         var indent = EditorGUI.indentLevel;
         EditorGUI.indentLevel = 0;
         
         var items = property.FindPropertyRelative ("items");
         string[] titles = new string[] { "Transform", "", "", "" };
         string[] props = new string[] { "transform", "^", "v", "-" };
         float[] widths = new float[] { .7f, .1f, .1f, .1f };
         float lineHeight = 18;
         bool changedLength = false;
         if (items.arraySize > 0)
         {
             
             for (int i=-1; i<items.arraySize; ++i) {
 
                 var item = items.GetArrayElementAtIndex (i);
                 
                 float rowX = x;
                 for (int n=0; n<props.Length; ++n)
                 {
                     float w = widths[n] * inspectorWidth;
                     
                     // Calculate rects
                     Rect rect = new Rect (rowX, y, w, lineHeight);
                     rowX += w;
                     
                     if (i == -1)
                     {
                         EditorGUI.LabelField(rect, titles[n]);
                     } else {
                         if (n==0)
                         {
                             EditorGUI.ObjectField(rect, item.objectReferenceValue, typeof(Transform), true);                        
                         } else {
                             if (GUI.Button (rect, props[n]))
                             {
                                 switch (props[n])
                                 {
                                 case "-":
                                     items.DeleteArrayElementAtIndex(i);
                                     items.DeleteArrayElementAtIndex(i);
                                     changedLength = true;
                                     break;
                                 case "v":
                                     if (i > 0) items.MoveArrayElement(i,i+1);
                                     break;
                                 case "^":
                                     if (i < items.arraySize-1) items.MoveArrayElement(i,i-1);
                                     break;
                                 }
 
                             }
                         }
                     }
                 }
                 
                 y += lineHeight + spacing;
                 if (changedLength)
                 {
                     break;
                 }
             }
              
         } else {
 
             // add button
             var addButtonRect = new Rect ((x + position.width) - widths[widths.Length-1]*inspectorWidth, y, widths[widths.Length-1]*inspectorWidth, lineHeight);
             if (GUI.Button (addButtonRect, "+")) {
                 items.InsertArrayElementAtIndex(items.arraySize);
             }
             
             y += lineHeight + spacing;
         }
 
         // add all button
         var addAllButtonRect = new Rect (x, y, inspectorWidth, lineHeight);
         if (GUI.Button (addAllButtonRect, "Assign using all child objects"))
         {
             var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
             var children = new Transform[ circuit.transform.childCount ];
             int n=0; foreach (Transform child in circuit.transform) children[n++] = child;
             System.Array.Sort( children, new TransformNameComparer() );
             circuit.waypointList.items = new Transform[children.Length];
             for (n=0; n<children.Length; ++n)
             {
                 circuit.waypointList.items[n] = children[n];
             }
         
         
         }
         y += lineHeight + spacing;
 
         // rename all button
         var renameButtonRect = new Rect (x, y, inspectorWidth, lineHeight);
         if (GUI.Button (renameButtonRect, "Auto Rename numerically from this order")) {
             var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
             int n=0; foreach (Transform child in circuit.waypointList.items) child.name = "Waypoint "+(n++).ToString("000");
 
         }
         y += lineHeight + spacing;
 
         // Set indent back to what it was
         EditorGUI.indentLevel = indent;
         EditorGUI.EndProperty ();
     }
     
     public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
     {
         SerializedProperty items = property.FindPropertyRelative ("items");
         float lineAndSpace = lineHeight + spacing;
         return 40 + (items.arraySize * lineAndSpace) + lineAndSpace;
     }
 
     // comparer for check distances in ray cast hits
     public class TransformNameComparer: IComparer
     {
         public int Compare(object x, object y) {
             return ((Transform)x).name.CompareTo(((Transform)y).name);
         }    
     }
     
 }
 #endif
 


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arpi2595 · Dec 31, 2016 at 01:02 PM 0
Share

Hey, did you find the solution? I have been working on the same problem for quite some time now.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gronzzz · Feb 26, 2017 at 12:55 PM

Hey look at gist: https://gist.github.com/anonymous/79556e49fbe8f38f47c8ff9fbcb83632

I've changed things for both smooth true and false. Check -1 % n params and mathf.repeat. As you pointed before, original script adds distance between first and last points as last, so it works like circuit. Also you must change p0 and p2 and p3 points in case of smooth route. On the original way they loop.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by NGC6543 · Feb 24, 2017 at 09:02 AM

In Fact, I had the same issue and created a new one by modifying the WaypointCircuit.cs.

Drag and drop the script, there would be errors but ignore them. add some empty gameobjects as children of the gameobject with this script. hit Rebulid Path and it will update the children gameobjects into nodes.

It has Editor handles to move nodes easily. You can select Closed circuit or Open route as you like. You can reverse its direction with a click. You can move the pivot transform to average/first node without moving its nodes.

Hope this helps! :)

 /*    WaypointRoute.cs by NGC6543(ngc6543@me.com)
  * 
  *  Based on the WaypointCircuit.cs from Unity's Standard Assets.
  *  It supports both Closed Circuit and Open Route.
  *  
  */
 
 using UnityEngine;
 using System.Collections;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 [ExecuteInEditMode]
 public class WaypointRoute : MonoBehaviour {
     public enum RouteType{ClosedCircuit, OpenRoute}
     [Header("Waypoint Circuit Settings", order=0)]
     public Transform[] nodes;
     public RouteType routeType = RouteType.ClosedCircuit;
 
     //public WaypointList waypointList = new WaypointList();
     [SerializeField] private bool smoothRoute = true;
     int numPoints;
     [SerializeField, HideInInspector] Vector3[] points;
     [SerializeField, HideInInspector] float[] distances;
 
     #if UNITY_EDITOR
     [Header("Editor & Gizmo")]
     public bool showName = false;
     [SerializeField] float editorVisualisationSubsteps = 100;
     [SerializeField] Color pathColor = Color.magenta;
     [SerializeField] Color nodeColor = Color.red;
     [SerializeField,Range(0.1f, 1f)] float nodeSize = 0.5f;
     [SerializeField] bool moveTo1stPos;
     [SerializeField] bool moveToCenter;
     [SerializeField] bool reverseDirection;
     public float NodeSize{get{return nodeSize;}}
     #endif
 
     public float Length { get; private set; }
 
     [System.Serializable]
     public class WaypointList
     {
         //public WaypointCircuit circuit;
         public Transform[] items = new Transform[0];
     }
 
     public struct RoutePoint
     {
         public Vector3 position;
         public Vector3 direction;
 
 
         public RoutePoint(Vector3 position, Vector3 direction)
         {
             this.position = position;
             this.direction = direction;
         }
     }
 
     //this being here will save GC allocs
     private int p0n;
     private int p1n;
     private int p2n;
     private int p3n;
 
     private float lerpRatio;
     private Vector3 P0;
     private Vector3 P1;
     private Vector3 P2;
     private Vector3 P3;
 
     // Use this for initialization
     private void Awake()
     {
         if (nodes.Length > 1)
         {
             CachePositionsAndDistances();
         }
         numPoints = nodes.Length;
     }
 
 
     public RoutePoint GetRoutePoint(float dist)
     {
         // position and direction
         Vector3 p1 = GetRoutePosition(dist);
         Vector3 p2 = GetRoutePosition(dist + 0.1f);
         Vector3 delta = p2 - p1;
         return new RoutePoint(p1, delta.normalized);
     }
 
 
     public Vector3 GetRoutePosition(float dist)
     {
         int index = 0;
 
         if (Length == 0)
         {
             Length = distances[distances.Length - 1];
         }
 
         if(routeType == RouteType.ClosedCircuit){
             dist = Mathf.Repeat(dist, Length);
             while (distances[index] < dist)
             {
                 ++index;
             }
 
             // get nearest two points, ensuring points wrap-around start & end of circuit
             p1n = ((index - 1) + numPoints)%numPoints;
             p2n = index;
 
             // found point numbers, now find interpolation value between the two middle points
 
             lerpRatio = Mathf.InverseLerp(distances[p1n], distances[p2n], dist);
 
             if (smoothRoute)
             {
                 // smooth catmull-rom calculation between the two relevant points
 
 
                 // get indices for the surrounding 2 points, because
                 // four points are required by the catmull-rom function
                 p0n = ((index - 2) + numPoints)%numPoints;
                 p3n = (index + 1)%numPoints;
 
                 // 2nd point may have been the 'last' point - a dupe of the first,
                 // (to give a value of max track distance instead of zero)
                 // but now it must be wrapped back to zero if that was the case.
                 p2n = p2n%numPoints;
 
                 P0 = points[p0n];
                 P1 = points[p1n];
                 P2 = points[p2n];
                 P3 = points[p3n];
 
                 return CatmullRom(P0, P1, P2, P3, lerpRatio);
             }else
             {
                 // simple linear lerp between the two points:
 
                 p1n = ((index - 1) + numPoints)%numPoints;
                 p2n = index;
 
                 return Vector3.Lerp(points[p1n], points[p2n], lerpRatio);
             }
         } else{
             dist = Mathf.Clamp(dist, 0f, Length);
             while (distances[index] < dist)
             {
                 ++index;
             }
 
             if(index < 2 || index == distances.Length-1){
                 if(index == 0) index = 1;
                 p1n = (index - 1);
                 p2n = index;
                 lerpRatio = Mathf.InverseLerp(distances[p1n], distances[p2n], dist);
                 return Vector3.Lerp(points[p1n], points[p2n], lerpRatio);
             }else{
                 p1n = (index - 1);
                 p2n = index;
 
                 // found point numbers, now find interpolation value between the two middle points
 
                 lerpRatio = Mathf.InverseLerp(distances[p1n], distances[p2n], dist);
 
                 if (smoothRoute)
                 {
                     // smooth catmull-rom calculation between the two relevant points
 
 
                     // get indices for the surrounding 2 points, because
                     // four points are required by the catmull-rom function
                     p0n = (index - 2);
                     p3n = (index + 1);
 
                     P0 = points[p0n];
                     P1 = points[p1n];
                     P2 = points[p2n];
                     P3 = points[p3n];
                     //Debug.Log(p0n +", "+ p1n +", "+ p2n +", "+ p3n);
                     return CatmullRom(P0, P1, P2, P3, lerpRatio);
                 }
                 else{
                     if(index == 0) index = 1;
                     p1n = (index - 1);
                     p2n = index;
 
                     return Vector3.Lerp(points[p1n], points[p2n], lerpRatio);
                 }
             }
         }
     }
 
     public float GetProgress(Vector3 position, out int crntNearestNodeID, int prevNearestNodeID = -1){
         //int index = 0;
 
         if (Length == 0)
         {
             Length = distances[distances.Length - 1];
         }
 
         int nearestNodeID = -1;
         if(prevNearestNodeID == -1){
             // find nearest node for once
             float dist = 99999f;
             for(int i=0 ; i< nodes.Length ; i++){
                 float temp = Vector3.SqrMagnitude(position-nodes[i].position);
                 if(temp < dist){
                     dist = temp;
                     nearestNodeID = i;
                 }
             }
             //Debug.Log("initial nearest node : " + nearestNodeID);
         }else{
             nearestNodeID = prevNearestNodeID;
         }
         int i0;
         int i1;
         Vector3 p0;
         Vector3 v0;
         Vector3 v1;
         Vector3 n ;
         float lerp=0;
         float result=0f;
 
         switch(routeType){
             case RouteType.OpenRoute :
                 i0 = nearestNodeID == numPoints ? numPoints -1 : nearestNodeID;
                 i1 = nearestNodeID + 1;
                 p0 = position - nodes[i0].position;
                 v0 = nodes[i0].position;
                 v1 = nodes[i1].position;
                 n = v1 - v0;
 
                 if(Vector3.Dot(p0,n) > 0){
                     // nearest node is backward and consistent!
                     lerp = Vector3.Dot(n, position-v0)/n.sqrMagnitude;
                     result = Mathf.Lerp(distances[i0], distances[i1] + (i1==0?Length:0), lerp);
                     //Debug.Log("nearest : " + i0 +", backward\n" +
                     //          "lerp : " + lerp +"\n" +
                     //          "result : " + result);
                     if(lerp > 0.5f) nearestNodeID = i1;
                 }else{
                     // nearest node is forward!
                     i0 = (nearestNodeID-1+numPoints)%numPoints;
                     i1 = nearestNodeID;
                     v0 = nodes[i0].position;
                     v1 = nodes[i1].position;
                     n = v1-v0;
 
                     lerp = Vector3.Dot(n, position-v0)/n.sqrMagnitude;
                     result = Mathf.Lerp(distances[i0], distances[i1] + (i1==0?Length:0), lerp);
                     //Debug.Log("nearest : " + i1 +", forward\n" +
                     //          "lerp : " + lerp +"\n" +
                     //          "result : " + result);
                 }
 
                 crntNearestNodeID = nearestNodeID;
                 break;
             case RouteType.ClosedCircuit :
                 // determine the closest node is forward / backward the nearest line.
                 // assume the nearest node is backward point
                 i0 = nearestNodeID;
                 i1 = (nearestNodeID+1)%numPoints;
                 p0 = position - nodes[i0].position;
                 v0 = nodes[i0].position;
                 v1 = nodes[i1].position;
                 n = v1 - v0;
                 if(Vector3.Dot(p0,n) > 0){
                     // nearest node is backward and consistent!
                     lerp = Vector3.Dot(n, position-v0)/n.sqrMagnitude;
                     result = Mathf.Lerp(distances[i0], distances[i1] + (i1==0?Length:0), lerp);
                     //Debug.Log("nearest : " + i0 +", backward\n" +
                     //          "lerp : " + lerp +"\n" +
                     //          "result : " + result);
                     if(lerp > 0.5f) nearestNodeID = i1;
                 }else{
                     // nearest node is forward!
                     i0 = (nearestNodeID-1+numPoints)%numPoints;
                     i1 = nearestNodeID;
                     v0 = nodes[i0].position;
                     v1 = nodes[i1].position;
                     n = v1-v0;
 
                     lerp = Vector3.Dot(n, position-v0)/n.sqrMagnitude;
                     result = Mathf.Lerp(distances[i0], distances[i1] + (i1==0?Length:0), lerp);
                     //Debug.Log("nearest : " + i1 +", forward\n" +
                     //          "lerp : " + lerp +"\n" +
                     //          "result : " + result);
                 }
 
                 crntNearestNodeID = nearestNodeID;
                 break;
 
             default :
                 crntNearestNodeID = nearestNodeID;
                 break;
         }
 
         return result;
     }
 
 
     public float GetProgressDistanceOnNode(int nodeIndex){
         //Debug.Log(name + " node count : " + nodes.Length +"\n" +
         //          "requested index : " + nodeIndex);
 
         try{
             return distances[nodeIndex];
         }catch (System.IndexOutOfRangeException e){
             return 999f;
         }
     }
 
     private Vector3 CatmullRom(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float i)
     {
         // comments are no use here... it's the catmull-rom equation.
         // Un-magic this, lord vector!
         return 0.5f*
                     ((2*p1) + (-p0 + p2)*i + (2*p0 - 5*p1 + 4*p2 - p3)*i*i +
                     (-p0 + 3*p1 - 3*p2 + p3)*i*i*i);
     }
 
 
     private void CachePositionsAndDistances()
     {
         // transfer the position of each point and distances between points to arrays for
         // speed of lookup at runtime
 
 
         float accumulateDistance = 0;
         switch(routeType){
             case RouteType.ClosedCircuit : 
 
                 points = new Vector3[nodes.Length + 1];
                 distances = new float[nodes.Length + 1];
 
                 for (int i = 0; i < points.Length; ++i)
                 {
                     var t1 = nodes[(i)%nodes.Length];
                     var t2 = nodes[(i + 1)%nodes.Length];
                     if (t1 != null && t2 != null)
                     {
                         Vector3 p1 = t1.position;
                         Vector3 p2 = t2.position;
                         points[i] = nodes[i%nodes.Length].position;
                         distances[i] = accumulateDistance;
                         accumulateDistance += (p1 - p2).magnitude;
                     }
                 }
             break;
             case RouteType.OpenRoute :
 
                 points = new Vector3[nodes.Length];
                 distances = new float[nodes.Length];
 
                 for (int j = 0; j < nodes.Length-1; ++j)
                 {
                     var t1 = nodes[j];
                     var t2 = nodes[j+1];
                     if (t1 != null && t2 != null)
                     {
                         Vector3 p1 = t1.position;
                         Vector3 p2 = t2.position;
                         points[j] = nodes[j].position;
                         distances[j] = accumulateDistance;
                         accumulateDistance += (p1 - p2).magnitude;
                     }
                 }
                 points[points.Length-1] = nodes[nodes.Length-1].position;
                 distances[distances.Length-1] = accumulateDistance;
                 break;
                 
             default :
                 break;
 
         }
 
     }
 
     #if UNITY_EDITOR
 
     void Update () {
         if(nodes.Length != transform.childCount)    UpdateNodes();
         if(moveTo1stPos){
             moveTo1stPos = false;
             MoveTo1stNode();
         }
         if(moveToCenter){
             moveToCenter = false;
             MoveToCenter();
         }
         if(reverseDirection){
             reverseDirection = false;
             ReverseDirection();
         }
     }
 
     private void OnDrawGizmos()
     {
         DrawGizmos(false);
     }
 
 
     private void OnDrawGizmosSelected()
     {
         DrawGizmos(true);
     }
 
 
     private void DrawGizmos(bool selected)
     {
         //waypointList.circuit = this;
 
         switch(routeType){
             case RouteType.ClosedCircuit :
                 if (nodes.Length > 1)
                 {
                     numPoints = nodes.Length;
 
                     CachePositionsAndDistances();
                     Length = distances[distances.Length - 1];
 
                     // draw path
                     Gizmos.color = pathColor;
                     Vector3 prev = nodes[0].position;
                     if (smoothRoute)
                     {
                         float step = Length/editorVisualisationSubsteps;
                         for (float dist = 0; dist < Length; dist += step)
                         {
                             Vector3 next = GetRoutePosition(dist + 1);
                             Gizmos.DrawLine(prev, next);
                             prev = next;
                         }
                         Gizmos.DrawLine(prev, nodes[0].position);
                     }
                     else
                     {
                         for (int n = 0; n < nodes.Length; ++n)
                         {
                             Vector3 next = nodes[(n + 1)%nodes.Length].position;
                             Gizmos.DrawLine(prev, next);
                             prev = next;
                         }
                     }
 
 
                 }
                 break;
             case RouteType.OpenRoute :
                 if(nodes.Length > 1){
                     numPoints = nodes.Length;
                     CachePositionsAndDistances();
                     Length = distances[distances.Length-1];
 
                     // draw path
                     Gizmos.color = pathColor;
                     Vector3 prev = points[0];
                     if(smoothRoute){
                         float step = Length/editorVisualisationSubsteps;
                         for(float dist = 0; dist < Length ; dist += step){
                             Vector3 next = GetRoutePosition(dist + 1);
                             Gizmos.DrawLine(prev, next);
                             prev = next;
                         }
                     }else{
                         for(int i=0 ; i<nodes.Length-1 ; i++){
                             Vector3 next = nodes[i+1].position;
                             Gizmos.DrawLine(prev, next);
                             prev = next;
                         }
                     }
                 }
                 break;
             default :
                 break;
         }
         // draw node
         Gizmos.color = nodeColor;
         foreach(Transform t in nodes){
             Gizmos.DrawCube(t.position, Vector3.one * nodeSize);
         }
     }
 
     public void UpdateNodes(){
         nodes = new Transform[transform.childCount];
         for(int i=0 ; i<transform.childCount ; i++){
             nodes[i] = transform.GetChild(i);
             nodes[i].name = "Node"+i;
         }
     }
 
     void MoveTo1stNode(){
         if(nodes.Length == 0){
             Debug.LogWarning("There are no path points yet.");
             return;
         }
         Vector3 offset = transform.position - nodes[0].position;
         transform.position = nodes[0].position;
         foreach(Transform t in nodes ){
             t.position += offset;
         }
     }
     void MoveToCenter(){
         if(nodes.Length == 0){
             Debug.LogWarning("There are no path points yet.");
             return;
         }
 
         Vector3 center = Vector3.zero;
         for(int i=0 ; i<nodes.Length ; i++){
             center += nodes[i].position;
         }
         center /= nodes.Length;
         Vector3 offset = transform.position - center;
         transform.position = center;
         foreach(Transform t in nodes ){
             t.position += offset;
         }
     }
     void ReverseDirection(){
         if(nodes.Length == 0){
             Debug.LogWarning("There are no nodes yet. Add child Transform.");
             return;
         }
 
         Transform[] temp = nodes;
         foreach(Transform t in temp){
             t.SetParent(null);
         }
         for(int i=temp.Length-1 ; i>-1 ; i--){
             temp[i].SetParent(transform);
         }
         UpdateNodes();
     }
     #endif
 }
 
 #if UNITY_EDITOR
 [CustomEditor(typeof(WaypointRoute)), CanEditMultipleObjects]
 public class WaypointRouteEditor : Editor{
     private const float handleSize = 0.08f;
     private const float pickSize = 0.06f;
 
     WaypointRoute myScript;
     Transform handleTransform;
 
     public override void OnInspectorGUI(){
 
         //        EditorGUILayout.HelpBox("Two Types of paths\n" +
         //            "1. No margin : It starts from the first path points.\n" +
         //            "2. With margin : it starts from the second path points.\n\n" +
         //            "Option 2 needs more than two path points. " +
         //            "This is useful if there are more than two paths they needed to be smoothly blended. " +
         //            "This can be achieved by positioning the first two path points of the later path " +
         //            "at the last two path points of the prior one", MessageType.Info);
         DrawDefaultInspector();
         myScript = target as WaypointRoute;
         if(GUILayout.Button("Rebuild Path"))
         {
             myScript.UpdateNodes();
         }
     }
     bool nodeSelected = false;
     int selectedNode = -1;
     GUIStyle normalStyle = new GUIStyle();
     void OnSceneGUI(){
         myScript = target as WaypointRoute;
         handleTransform = myScript.transform;
         Transform[]  nodes = myScript.nodes;
 
         normalStyle.normal.textColor = Color.green;
         normalStyle.fontSize = 14;
         Vector3 camPos = Camera.current.transform.position;
         Vector3 camUp = Camera.current.transform.up;
         // node handle
         Handles.color = new Color(0,1f,0,1f);// green
         for(int i=0 ; i<nodes.Length ; i++){
             float size = HandleUtility.GetHandleSize(nodes[i].position);
             Vector3 temp = Vector3.Cross((nodes[i].position - camPos),camUp).normalized * myScript.NodeSize;;
 
             if(myScript.showName)
                 Handles.Label(myScript.transform.position, myScript.name, normalStyle);
 
             Handles.Label(nodes[i].position + temp, i.ToString(), normalStyle);
                    
             if(Handles.Button(nodes[i].position, Quaternion.identity, size * handleSize, size * pickSize, Handles.DotCap )){
                 nodeSelected = true;
                 selectedNode = i;
                 break;
             }
             if(nodeSelected){
                 EditorGUI.BeginChangeCheck();
                 Vector3 point = Handles.DoPositionHandle(nodes[selectedNode].position, Quaternion.identity);
                 if(EditorGUI.EndChangeCheck()){
                     Undo.RecordObject(myScript, "move node");
                     EditorUtility.SetDirty(myScript);
                     myScript.nodes[selectedNode].position = point;
                 }
             }
         }
     }
 }
 
 #endif
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Standard assets broken 1 Answer

Using AirBrakes on mobile device with Standard Assets AircraftController 0 Answers

Running with the Third Person Controller Standard Assets 0 Answers

Modify Standard Asset --> MultipurposeCameraRig (add manual rotation) 0 Answers

How to change script's value for only one object? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges