Hello am struggling with the ol’ js AIcar_script :°)
Thanks to Guyt for converting it in c# in another post…
BUT…
I still can’t get it work in C#, when the js is doing the routine…
I get “Obj ref not set to an instance” pointing at this line :
Vector3 RelativeWaypointPosition = transform.InverseTransformPoint( new Vector3(waypoints[currentWaypoint].transform.position.x, transform.position.y, waypoints[currentWaypoint].transform.position.z ) );
I tried other ways, but it seems the c# script can’t retrieve the potentialWaypoints from the GameObject. :°(
I hope someone can help since it’s the only example of my knowledge using “getcomponentsinchildren” to populate the array, rather than instantiating the waypoints manually.
Below the condensed version of the waypoint system :
Thank for your help!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AICar : MonoBehaviour {
public GameObject waypointContainer;
private List <Transform> waypoints;
public int currentWaypoint = 0;
void Awake () {
GetWaypoints();
}
void GetWaypoints ()
{
if (waypointContainer!=null)
{
Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren <Transform> ();
waypoints = new List<Transform>();
foreach (Transform potentialWaypoint in potentialWaypoints)
{
if (potentialWaypoint != waypointContainer.transform)
waypoints.Add(potentialWaypoint);
}
}
}
void Cruise () {
Vector3 RelativeWaypointPosition = transform.InverseTransformPoint( new Vector3(waypoints[currentWaypoint].transform.position.x, transform.position.y, waypoints[currentWaypoint].transform.position.z ) );
Vector3 RelativeWaypointDir = ((waypoints[currentWaypoint].transform.position) - transform.position);
float currentAngle = Vector3.Angle(RelativeWaypointDir, transform.forward);
// then IA torque, steering, or animated NPC can use the previous these values
if ( RelativeWaypointPosition.magnitude < 15 )
{
currentWaypoint ++;
if ( currentWaypoint >= waypoints.Count )
currentWaypoint = 0;
}
}
}