- Home /
AI car Script problem
I have an problem whit A.I car.
How do I stop the car to drive backwards after a crash and return to its normal position and continue driving on your way?
here is the code:
var FrontLeftWheel : WheelCollider;
var FrontRightWheel : WheelCollider;
var EngineTorque : float = 600.0;
var waypointContainer : GameObject;
private var waypoints : Array;
private var currentWaypoint : int = 0;
private var inputSteer : float = 0.0;
private var inputTorque : float = 0.0;
function Start () {
rigidbody.centerOfMass.y = -1.5;//-1.5
GetWaypoints(); }
function FixedUpdate() {
rigidbody.drag = rigidbody.velocity.magnitude / 250;
NavigateTowardsWaypoint();
FrontLeftWheel.motorTorque = EngineTorque * inputTorque;
FrontRightWheel.motorTorque = EngineTorque * inputTorque;
FrontLeftWheel.steerAngle = 20 * inputSteer;
FrontRightWheel.steerAngle = 20 * inputSteer;
}
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 ) //< 0.5 { inputTorque = RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs(inputSteer);//Abs
} else { inputTorque = 0.0; }
if ( RelativeWaypointPosition.magnitude < 15 ) //20 { currentWaypoint ++;
if ( currentWaypoint >= waypoints.length ) { currentWaypoint = 0;
}
}
}
Please format your code properly! Edit your post, select the whole script and press the 101010 button. It's much easier for people to help you if they can read your code :)
Thank you, I myself was also confused by what I wrote yesterday :)
Answer by roamcel · Aug 04, 2011 at 08:58 AM
I'm sorry but I couldn't arse myself to decipher the code, but I have a simple suggestion:
Install a COLLIDER in your car (supposed that you haven't done it already), and in your vehicle script you implement the appropriate hit or trigger function, as detailed in the manual: http://unity3d.com/support/documentation/ScriptReference/Collider.html
I have attached the collider in the car, but still, when a collision with another car and turned the waypoint position. The car was still running in fallback position
If I'm not mistaken, what happens is that the collision changes the actual bearing of the car, so the FIRST direction calculations get offset. I think that after a collision you just need to make another call to NavigateTowardsWaypoint to assure that the correct new bearing is calculated.
Your answer
Follow this Question
Related Questions
AI Car Problem 1 Answer
Ai turning problem on Android - Question solved 0 Answers
How To Add AI To Car/Racer? 4 Answers
How to implement traffic system efficiently in Unity game to run smoothly on mobile device? 1 Answer
Car AI Avoiding Obstacle 0 Answers