/// <summary> /// Getting a position on the spline based on our position in the spline /// </summary> /// <param name="off">Offseted distance from this</param> /// <returns>Position in the spline</returns> Vector3 GetCurrPos(float off) { Vector3 pos = Vector3.zero; //adding the offset to our current pos float seg = currSeg + off; int point = _currPoint; int subway = _currSubway; //adjusting other values based on the offset while (seg >= 1) { seg--; int[] ways = GetNextPoint(ref point); point = Mathf.Clamp(point, 0, spline.Length - 1); SetNextPointPos(); if (ways.Length == 0) { return(nextPointPos); } else if (ways.Length == 1) { subway = 0; } else { return(spline[point].position); } } pos = spline.GetPointAtTime(seg, point, subway, reverse); return(pos); }
/// <summary> /// Drawing the spline in the editor /// </summary> void OnDrawGizmos() { if (!spline) { return; } Gizmos.color = splineColor; float prevTime = 0; Vector3 pointA; Vector3 pointB = new Vector3(); Waypoint[] waypoints = spline.waypoints; bool notSelected = UnityEditor.Selection.gameObjects.Length == 0 || UnityEditor.Selection.gameObjects[0] != gameObject; if (notSelected) { SetWorldPositions(); } float rpp = 0.1f; // resolution per point spline.SetSegments(); for (int i = 0; i < waypoints.Length; i++) { if (notSelected) { Gizmos.color = pointColor; Gizmos.DrawSphere(waypoints[i].position, 0.2f); } Gizmos.color = splineColor; for (int s = 0; s < waypoints[i].subways.Length; s++) { rpp = waypoints[i].segments[s]; for (float t = rpp; t <= 1; t += rpp) { pointA = spline.GetPointAtTime(prevTime, i, s); pointB = spline.GetPointAtTime(t, i, s); Gizmos.DrawLine(pointA, pointB); prevTime = t; } prevTime = 0; Gizmos.DrawLine(pointB, waypoints[waypoints[i].subways[s]].position); } } }
/// <summary> /// draws the arrows on the spline /// arrow will be pointing to the direction the player will move to /// </summary> /// <param name="point">points to draw arrows at</param> void DrawArrow(int point) { for (int s = 0; s < spline.GetSubwaysLength(point); s++) { float size = HandleUtility.GetHandleSize(spline.GetPosition(point)); Handles.ArrowCap(0, spline.GetPosition(point), Quaternion.LookRotation(spline.GetPointAtTime(0.2f, point, s) - spline.GetPosition(point)), size); } }