How to have realistic collisions with AI in a racing game?

I am making a racing game in Unity. I started off following the Car Tutorial on the Unity website, and have added some basic AI using the SeekSteer script found here:
[SeekSteer Script][1]unifycommunity.com

The problem with that script is that the cars do not respond well to any sort of collision. Either they stay directly on their path or they move upwards off the ground, but continue on the path and slowly lower to the ground after some time.

I thought a good way to prevent this would be to momentarily disable the script upon collision for X amount of time, then reenable it shortly afterwards. However, I am having trouble doing this, and can’t figure out the best way to do so.

Here is the collision code:

void OnTriggerEnter(Collider other)
	{
		if(other.name == "Car")
		{
			collision = true;
			sleep();    			
		}
	}

IEnumerator sleep()
	{
		yield return new WaitForSeconds(sleepTime);
	}

And the update method:

protected void Update()
    {
		if (!collision)
		{
			if(useRigidbody)
            	rigidmember.velocity = currentHeading * speed;
        	else
           		xform.position +=currentHeading * Time.deltaTime * speed;
       	 	if(faceHeading)
            	xform.LookAt(xform.position+currentHeading);      
        	if(Vector3.Distance(xform.position,waypoints[targetwaypoint].position)<=waypointRadius)
        	{
            	targetwaypoint++;
				speed = Random.Range(0.69F, 0.71F);
            	if(targetwaypoint>=waypoints.Length)
            	{
                	targetwaypoint = 0;
                	if(!loop)
                    	enabled = false;
            	}     
			}
			
		}
		else if (collision)
		{
			sleep ();
			collision = false;
		}
}

I can’t figure out how to fix this problem.

Any help is greatly appreciated!!!

The problem is how your code is moving things.

rigidmember.velocity = currentHeading * speed;//THIS IS NOT HOW TO DO IT

Is setting the velocity and ignoring any affect the acceleration of gravity might be having. Instead you will want to use AddForce. It’s OK to set velocity directly when certain events happen, but setting it in update will cause it to basically ignore the physics.

Note if you keep adding force over and over again, you will start going very quick unless there is also drag or friction. The nice thing is that this can have your car slide to a nice stop if it turns off its motor.

Your other problem is that you are having it move towards the current heading no matter if it is on the ground or not. So even if it was miles up in the air, it would still be moving itself towards that point.

The easy way to do this is to perform a raycast towards the ground and see if it hits the ground.

So you could add something like:

var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100)) {
    var distanceToGround = hit.distance;
    if(distanceToGround > WHATEVER THE HEIGHT OF YOUR CAR IS){
         //All of your update code
    }
}

So it performs a raycast downwards. If it doesn’t hit anything or if the distance is too large, your code that makes the car “drive” is ignored.

Read up some tutorials on youtube this will explain a lot better,