I create a small traffic system with waypoints. So the AI cars drives based on waypoints.
But I have a problem, that all of the cars always starts from the waypoints_0 position. It become looks like a car racing, where all of the cars starts from the same position.
If i moved the car position to somewhere in the road, (not at waypoints_0) , it can’t be move.
I want to make them start from random position. So I can place a car in the somewhere position near a waypoint (for example: at waypoints_1, waypoints_3 , etc.) and it can move to their next waypoints.
Please help me, what i have to do to modify my script?
This is my AI script:
Thank you before and after…
function GetWaypoints () {
var potentialWaypoints : Array = waypointContainer.GetComponentsInChildren( Transform );
waypoints = new Array();
for ( var potentialWaypoint : Transform in potentialWaypoints ) {
if ( potentialWaypoint != waypointContainer.transform ) {
waypoints[ waypoints.length ] = potentialWaypoint;
}
}
}
function NavigateTowardsWaypoint () {
var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( Vector3(
waypoints[currentWaypoint].position.x,
transform.position.y,
waypoints[currentWaypoint].position.z ) );
inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
if ( Mathf.Abs( inputSteer ) < 0.5 ) {
inputTorque = RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs( inputSteer );
inputBreak = 0.0;
}
if ( RelativeWaypointPosition.z < 20 ) {
GetCarInfront();
inputTorque = (-inputSteer*0.8);
inputBreak = (rigidbody.velocity.magnitude * 10);
}
if ( RelativeWaypointPosition.z < 20 ) {
GetCarInfront();
inputTorque = 0.0;
inputBreak = (rigidbody.velocity.magnitude * 10);
}
if ( RelativeWaypointPosition.z < 10 ) {
GetCarInfront();
inputTorque = 0.0;
inputBreak = (rigidbody.velocity.magnitude * 10);
}
if ( RelativeWaypointPosition.magnitude < 15 ) {
currentWaypoint ++;
if ( currentWaypoint >= waypoints.length ) {
currentWaypoint = 0;
}
}
}