Hi, I’m making a traffic AI script that read gets waypoints from a path object, now I am trying to get a random path to start with from a path container object(in the same way as I get the waypoints), but I get this error:
“InvalidCastException: Cannot cast from source type to destination type.”
Basically the GetFirstPath() function is supposed to work similar to GetWaypoints().
// AI Traffic car script //
///////////////////////////
var WheelFR: WheelCollider;
var WheelFL: WheelCollider;
var WheelRR: WheelCollider;
var WheelRL: WheelCollider;
var MaxTorque= 15;
var BrakeTorque=20;
var MaxAngle=30;
private var Speed: float;
private var KPH: float;
var MaxSpeedKPH=45;
var SteeringAngle;
var DriveTorque;
var CenterOfMass=-0.7;
var waypointContainer : GameObject;
private var waypoints : Array;
private var currentWaypoint : int;
private var SpawnWaypoint : int;
private var FirstPath: int;
private var NextPathChosen:int;
var pathContainer : GameObject;
var paths : Array;
private var inputSteer : float = 0.0;
private var inputTorque : float = 0.0;
private var Obstacle:boolean;
private var Timer:float;
private var Chosen:boolean;
function Start () // initializing
{
// assign waypoint container
GetFirstPath();
// pick random path/waypointContainer
FirstPath = Random.Range(0, (paths.length-1));
waypointContainer= paths[FirstPath];
//lower COG
rigidbody.centerOfMass.y = CenterOfMass;
// get the waypoints
GetWaypoints();
}
// MAIN
function Update ()
{
}
// WAYPOINTS
function FixedUpdate () {
NavigateTowardsWaypoint();
}
function GetWaypoints () {
// Now, this function basically takes the container object for the waypoints, then finds all of the transforms in it,
// once it has the transforms, it checks to make sure it's not the container, and adds them to the array of waypoints.
var potentialWaypoints : Array = waypointContainer.GetComponentsInChildren( Transform );
waypoints = new Array();
for ( var potentialWaypoint : Transform in potentialWaypoints ) {
if ( potentialWaypoint != waypointContainer.transform ) {
waypoints[ waypoints.length ] = potentialWaypoint;
}
}
}
function GetFirstPath () {
// get the first path from a path container
var potentialPaths : Array = pathContainer.GetComponentsInChildren( Transform );
paths = new Array();
for ( var potentialPath : Transform in potentialPaths ) {
if ( potentialPath != pathContainer.transform ) {
paths[ paths.length ] = potentialPath;
}
}
}
function NavigateTowardsWaypoint () {
// now we just find the relative position of the waypoint from the car transform,
// that way we can determine how far to the left and right the waypoint is.
var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( Vector3(waypoints[currentWaypoint].position.x, transform.position.y, waypoints[currentWaypoint].position.z ) );
// by dividing the horizontal position by the magnitude, we get a decimal percentage of the turn angle that we can use to drive the wheels
inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
// now we do the same for torque, but make sure that it doesn't apply any engine torque when going around a sharp turn...
if ( Mathf.Abs( inputSteer ) < 0.5 && !Obstacle) {
inputTorque = 0.5; //RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs( inputSteer );
}
if (Mathf.Abs( inputSteer ) > 0.5 || Obstacle){
//inputTorque = 0;
}
// this just checks if the car's position is near enough to a waypoint to count as passing it, if it is, then change the target waypoint to the
// next in the list.
if ( RelativeWaypointPosition.magnitude < 1 ) {
currentWaypoint ++;
//if ( currentWaypoint < waypoints.length ) {
//}
//
}
}