Hi all,
So I’m having problem with implementing Unity Standard Asset WaypointProgressTracker.cs for my car AI. What I’'m doing is I have 2 waypoint containers, each have 1 waypoint empty game object, that have the script WaypointCircuit.cs, finish and start. (this is a drag race btw so it’s point to point pretty much).
In the car prefab I have an empty game object child as a target for the CarAIControl (also in Unity Standard Asset) and the target for WaypointProgressTracker.
And this is the sequence I’m implementing things and I think that’s causing the problem:
- The AI car will be in point to point mode and stopWhenReachedTarget.
- In my race manager script, I have reference to 2 waypoint containers. On start the race manager will feed the start line waypoint container to the WaypointProgressTracker attached in the car prefab, so the car will move to the start point and stop because stopWhenReachedTarget. the waypoint container is fed into “circuit” variable of the WaypointProgressTracker
- After hitting a trigger, temporary set Driving on the AIControl to false so it stays in the starting line.
- feed the finish waypoint container to the WaypointProgressTracker. enable driving after countdown.
-The weird thing is, I’m having null pointer exception but the AI runs fine. It still went to and stopped and start, and went to and stopped at finish.
NullReferenceException: Object reference not set to an instance of an object
WaypointRoute.GetRoutePosition (Single dist) (at Assets/Script/WaypointRoute.cs:65)
WaypointRoute.GetRoutePoint (Single dist) (at Assets/Script/WaypointRoute.cs:52)
WaypointTracker.Update () (at Assets/Script/WaypointTracker.cs:140)
NullReferenceException: Object reference not set to an instance of an object
WaypointRoute.GetRoutePosition (Single dist) (at Assets/Script/WaypointRoute.cs:65)
WaypointTracker.OnDrawGizmos () (at Assets/Script/WaypointTracker.cs:159)
UnityEditor.DockArea:OnGUI()
After chasing down the problem, this is my hypothesis:
- The waypointProgressTracker takes a circuit, which means the waypoint containers must have 2 or more waypoints to be considered as a circuit and to have a route. That is why I’m having trouble with these 2 lines:
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;
}
- Because there is no route, these two function return null. So when they are called in this function in WaypointProgressTracker in Update(), it’s throwing the error:
if (circuit != null)
{
// point to point mode. Just increase the waypoint if we're close enough:
Vector3 targetDelta = target.position - transform.position;
if (targetDelta.magnitude < pointToPointThreshold)
{
progressNum = (progressNum + 1) % circuit.Waypoints.Length;
}
target.position = circuit.Waypoints[progressNum].position;
target.rotation = circuit.Waypoints[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;
- Another thing is might be I’m swapping Circuit on the fly, so at the time in between there is nothing for the WaypointTracker to track
So.
TLDR: No route, nothing to track waypoints, null exception.
Is there a way to fix this will still using the WaypointTracker, as I want this script to handle waypoint for the AI to head to and track the progress, while still want the AI to stop when reached target. (If I use a route with 2 point then the AI will just stop at the first waypoint and refuse to move to the next one. If I don’t enable StopWhenReachedTarget then the AI will not stop when reached the second finish waypoint when will go around 2 points)
I think somebody else also had this problem but still unsolved:
I know it’s a long post for a morning, but please help me. I’ll upload the full script to you if you need it