3 Replies - 2718 Views - Last Post: 23 April 2013 - 04:50 AM Rate Topic: -----

#1 tntcenturion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-March 12

[Unity3D] How to make a better car AI ?

Posted 22 April 2013 - 06:32 AM

Sorry for asking too much, but I really need this improvement on my game. And this is the current AI logic :
Posted Image
This is the problem on my AI kart
  • AI1 was crashing the wall, and stopped working
  • AI2 should be able to move in the right way
  • but the AI2 crash himself to AI1 car
  • Now they are stuck each other
  • I call them to respawn, so they could see the track again
  • AI1 drive reversing endlessly, and AI2 still standing or didn't move anywhere

If you still don't understand how does it happens, you can see it here.

I want to make it like this
  • AI should be able to avoid each other (not crashing each other).
  • AI shouldn't stop forever / reverse forever everytime after respawn or before respawn.


And this is my code
function NavigateTowardsWaypoint(){ //this thing runs on Update function
	//WPObj is the coordinate / location of current waypoint.

	var relativePosition : Vector3;
	var hit : RaycastHit;
	
	if(!alternativeWaypointer){
		relativePosition = transform.InverseTransformPoint(Vector3(wpObj.transform.position.x,transform.position.y,wpObj.transform.position.z)); //This is the main controller of the AI
	}else{
		relativePosition = transform.InverseTransformPoint(Vector3(raycaster*wpObj.transform.position.x,transform.position.y,wpObj.transform.position.z)); 
	}
	
	if(Physics.SphereCast(transform.position,retardnessOffset,transform.forward,hit,retardnessOffset)){
		if(hit.transform != this.transform){
			if(hit.collider.tag == "Player" || hit.collider.tag == "Opponent"){
				alternativeWaypointer = true;
				raycaster = -hit.point.x / 3;
				//this is used when the AI were too close with other AI or player.
			}else alternativeWaypointer = false;
		}
	}
	
	if(retarded)inputSteer = relativePosition.x / relativePosition.magnitude; //this is the main problem of the AI
	else inputSteer = Mathf.Atan2(relativePosition.x,relativePosition.z); // this is the main problem of the AI (i made it 2 methods, if retarded method didn't work, i set them retarded = false
	
	if(!alternativeWaypointer){
		if(Mathf.Abs(inputSteer) < .5)inputTorque = relativePosition.z / relativePosition.magnitude; //calculate ideal speed when turning straight track.
		else{ 
			if(!isReversing)inputTorque = 0.2; //if it's not reversing, this is the speed when turning sharp track.
			else inputTorque = -0.3; // reverse them, but not too fast.
		}
	}else{
		if(!isReversing)inputTorque = 0.2;
		else inputTorque = -0.3;
	}
}


you might need this (scripting references)
- RaycastHit
- Mathf.Abs
- Mathf.Atan2
- Physics.Spherecast
- Transform.InverseTransformPoint

I really need this help.

Thanks for any answers.

Is This A Good Question/Topic? 0
  • +

Replies To: [Unity3D] How to make a better car AI ?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [Unity3D] How to make a better car AI ?

Posted 22 April 2013 - 12:43 PM

You're not using proper AI techniques here, and that is why things are not working. What should be happening is:

1. Each car should be chasing the closest car ahead of it.
2. Each car should be following a path that is normally represented as the 'racing line'.
3. Each car should be treating a car that is near enough as an obstacle and avoid it by steering around it.

I don't know how much you know about AI but there is a kind of path planning, obstacle avoidance with some basic flocking activity that you need to implement. Then you will need to look at very basic physics in the form of collision detection and response.

This is non-trivial.
Was This Post Helpful? 1
  • +
  • -

#3 tntcenturion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-March 12

Re: [Unity3D] How to make a better car AI ?

Posted 23 April 2013 - 01:36 AM

View PostButchDean, on 22 April 2013 - 12:43 PM, said:

You're not using proper AI techniques here, and that is why things are not working. What should be happening is:

1. Each car should be chasing the closest car ahead of it.
2. Each car should be following a path that is normally represented as the 'racing line'.
3. Each car should be treating a car that is near enough as an obstacle and avoid it by steering around it.

I don't know how much you know about AI but there is a kind of path planning, obstacle avoidance with some basic flocking activity that you need to implement. Then you will need to look at very basic physics in the form of collision detection and response.

This is non-trivial.


Yeah, the AI following the path correctly, but sometimes they can crash something like wall, that's where the problem happen. After they crashes the wall, I set them to respawn (if i don't set them respawn, they'll just standing and looking the wall forever or reversing forever), and after they respawn, they still just standing or reversing forever, it just like they didn't see the road.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [Unity3D] How to make a better car AI ?

Posted 23 April 2013 - 04:50 AM

Yes, but I've listed above what you need to look into to fix your problems. Racing games are challenging to develop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1