x


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;

}

}

}
more ▼

asked Aug 04 '11 at 04:03 AM

ourchat gravatar image

ourchat
11 5 5 6

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 :)

Aug 04 '11 at 05:33 AM JUnityer

Thank you, I myself was also confused by what I wrote yesterday :)

Aug 04 '11 at 03:15 PM ourchat
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Aug 04 '11 at 08:58 AM

roamcel gravatar image

roamcel
1.2k 37 40 44

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

Aug 04 '11 at 03:32 PM ourchat

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.

Aug 04 '11 at 08:20 PM roamcel
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x959
x418
x83
x36

asked: Aug 04 '11 at 04:03 AM

Seen: 1514 times

Last Updated: Aug 06 '11 at 06:53 AM