Hereās my ābread and butterā type implementation - very similar to Jason and Cridās. I pretty much implemented it at the end of the last lecture, as it seemed a logical next step.
Focused on readability by caching references and extracting a couple of repeated lines. This keeps the drawing logic clean. The only ugly bit is the transform.childCount - 1 check, but abstracting that away felt like overkillā¦
Keeping it readable is also doing my future self a favour (heās always grateful to me when I take the time to do this )
public class PatrolPath : MonoBehaviour
{
[SerializeField] float gizmoSize = 0.25f;
private void OnDrawGizmos()
{
var previousWaypoint = new Vector3();
var firstWaypoint = new Vector3();
for (int i = 0; i < transform.childCount; i++)
{
var currentWaypoint = transform.GetChild(i).transform.position;
DrawWaypoint(currentWaypoint);
if (i == 0)
{
firstWaypoint = currentWaypoint;
}
else
{
DrawLine(previousWaypoint, currentWaypoint);
}
if (i == transform.childCount - 1)
{
DrawLine(firstWaypoint, currentWaypoint);
continue;
}
previousWaypoint = currentWaypoint;
}
}
private static void DrawLine(Vector3 from, Vector3 to)
{
Gizmos.color = Color.green;
Gizmos.DrawLine(to, from);
}
private void DrawWaypoint(Vector3 centre)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(centre, gizmoSize);
}
}
here is my Attemp in drawing path loop
void OnDrawGizmos()
{
Transform previoustWayPoint;
Transform nextWayPoint ;
for (int i = 0; i < transform.childCount; i++)
{
Gizmos.DrawSphere(transform.GetChild(i).position ,gizmosSphereRadius);
previoustWayPoint = transform.GetChild(i);
nextWayPoint = transform.GetChild((i + 1)% transform.childCount);
Gizmos.DrawLine(previoustWayPoint.position , nextWayPoint.position);
}
}
I did this right after you said DrawLine lol. I changed the color for the final line to visualize the end and start points easier. I also set it up a bit differently without needing another variable. Any reason you wouldnāt do it this way? (asking if it could potentially break anything or isnāt as efficient)
private void OnDrawGizmos() {
Gizmos.color = Color.red; # First Waypoint/Line Color
for (int i = 0; i < transform.childCount; i++)
{
Vector3 currentPoint = GetWayPoint(i);
Vector3 nextPoint = GetWayPoint(GetNextIndex(i));
Gizmos.DrawSphere(currentPoint, waypointGizmosRadius);
Gizmos.DrawLine(currentPoint, nextPoint);
Gizmos.color = Color.white; # Regular Color
}
}