- Home /
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]http://www.unifycommunity.com/wiki/index.php?title=SeekSteer
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!!!
basically, every single thing you re doing is wrong :--)
fortunately, modern video games have a whole "physics engine", you DO NOT yourself EVER, EVER, EVER have to do ANYTHING. You NEVER set the velocity, or calculate anything. You make REAL, ACTUAl objects, and apply forces, and the game engine does EVERYTHING else.
This must be the #1 beginner mistake in game engines lately !!
"How do I calculate a parabola..."
Answer by Sapidus3 · Jun 17, 2012 at 07:13 AM
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.
Answer by reptilebeats · Jun 17, 2012 at 08:07 AM
Read up some tutorials on youtube this will explain a lot better,
Your answer
Follow this Question
Related Questions
Movement around a huge object by avoiding obstacles 1 Answer
Distribute terrain in zones 3 Answers
Resize Array Based on Value 2 Answers
Multiple Cars not working 1 Answer
Collider Vision AI question. Solved! 0 Answers