//public GameObject children; private void Start() { system = GameObject.FindGameObjectWithTag("WaypointSystem"); Transform[] circuit; if (loaded) { circuit = system.GetComponentsInChildren <Transform>(); } else { circuit = GetComponentsInChildren <Transform>(); } //Array.Sort(circuit, new TransformNameComparer()); distances = new float[waypointList.Length + 1]; waypointList = circuit; Waypoints = new WaypointCircuitAuto(waypointList); CachePositionsAndDistances(); numPoints = waypointList.Length; }
/*public class TransformNameComparer : IComparer * { * public int Compare(object x, object y) * { * //return ((Transform)x).name.CompareTo(((Transform)y).name); * } * }*/ public void AddChildren() { Transform[] circuit = this.GetComponentsInChildren <Transform>(); /*var children = new Transform[circuit.transform.childCount]; * int n = 0; * foreach (Transform child in circuit.transform) * { * children[n++] = child; * } * 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]; * }*/ //Array.Sort(circuit, new TransformNameComparer()); waypointList = circuit; Waypoints = new WaypointCircuitAuto(waypointList); }
// reset the object to sensible values /*public void Reset() * { * progressDistance = 0; * progressNum = 0; * if (progressStyle == ProgressStyle.PointToPoint) * { * target.position = circuit.waypointList[progressNum].rotation; * target.rotation = circuit.Waypoints[progressNum].rotation; * } * }*/ private void Update() { if (init) { if (progressStyle == ProgressStyle.SmoothAlongRoute) { // determine the position we should currently be aiming for // (this is different to the current progress position, it is a a certain amount ahead along the route) // we use lerp as a simple way of smoothing out the speed over time. if (Time.deltaTime > 0) { speed = Mathf.Lerp(speed, (lastPosition - transform.position).magnitude / Time.deltaTime, Time.deltaTime); } target.position = circuit.GetRoutePoint(progressDistance + lookAheadForTargetOffset + lookAheadForTargetFactor * speed) .position; target.rotation = Quaternion.LookRotation( circuit.GetRoutePoint(progressDistance + lookAheadForSpeedOffset + lookAheadForSpeedFactor * speed) .direction); // get our current progress along the route progressPoint = circuit.GetRoutePoint(progressDistance); Vector3 progressDelta = progressPoint.position - transform.position; if (Vector3.Dot(progressDelta, progressPoint.direction) < 0) { progressDistance += progressDelta.magnitude * 0.5f; } lastPosition = transform.position; } else { // point to point mode. Just increase the waypoint if we're close enough: Vector3 targetDelta = target.position - transform.position; if (targetDelta.magnitude < pointToPointThreshold) { progressNum = (int)((progressNum + 1) % circuit.Waypoints.Length); } target.position = circuit.waypointList[progressNum].position; target.rotation = circuit.waypointList[progressNum].rotation; // get our current progress along the route progressPoint = circuit.GetRoutePoint(progressDistance); Vector3 progressDelta = progressPoint.position - transform.position; if (Vector3.Dot(progressDelta, progressPoint.direction) < 0) { progressDistance += progressDelta.magnitude; } lastPosition = transform.position; } } else { if (wp.GetComponent <WaypointCircuitAuto>().ready) { circuit = wp.GetComponent <WaypointCircuitAuto>().Waypoints; init = true; } } }