Post news RSS Waypoint system

How to make a simple waypoint system for a car racing game?

Posted by on

There are quite a few of methods for making a waypoint system, but in CarEdu, decided to go with such a scheme:

Waypoint system flowchart

So, the Player1 (the 1st car) to check Waypoint2 must first enter and reach the Waypoint1 trigger, and to reach Waypoint3 the Waypoint2 trigger, ASO. That makes cheating the system by driving all the way back the track fairly impossible and compels the player to go from one point to another in correct order. ;-) It also lays some groundwork for time trial mode. Reaching the final waypoint also increases the number of laps made by the 1st player, and unchecks all reached waypoints.

When the 1st player reaches the final waypoint, and has made as many laps as required, the race is finished by him. The second player will have to made it to the finish line, though. (On a side note, Time.timeScale seen below won't be in the final script if everything goes well.)

Waypoint object Test waypoints

Below the experimental code. Note that these variables (Car1Waypoint1, ASO) will be replaced with arrays to keep down the clutter (and print functions will be removed):

var waypoint : Collider;
static var Car1Lap = 0;
static var Car2Lap = 0;
static var lapsToMake = 1;
static var Car1Waypoint1 = false; static var Car1Waypoint2 = false; static var Car1Waypoint3 = false; static var Car1Waypoint4 = false; 
static var Car1FinalWaypoint = false;
static var Car2Waypoint1 = false; static var Car2Waypoint2 = false; static var Car2Waypoint3 = false; static var Car2Waypoint4 = false; 
static var Car2FinalWaypoint = false;

function OnTriggerExit (other : Collider)
{
	if(other.attachedRigidbody.name == "Car1")
	{
		if(Car1Lap <= lapsToMake &amp;&amp; Car1FinalWaypoint == false)
		{
			if(Car1Waypoint1 == false &amp;&amp; waypoint.gameObject.name == "Waypoint1")
			{
			Car1Waypoint1 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car1 lap: " + Car1Lap);
			}
			else if(Car1Waypoint1 == true &amp;&amp; waypoint.gameObject.name == "Waypoint2" )
			{
			Car1Waypoint2 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car1 lap: " + Car1Lap);
			}
			else if(Car1Waypoint2 == true &amp;&amp; waypoint.gameObject.name == "Waypoint3")
			{
			Car1Waypoint3 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car1 lap: " + Car1Lap);
			}
			else if(Car1Waypoint3 == true &amp;&amp; waypoint.gameObject.name == "Waypoint4")
			{
			Car1Waypoint4 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car1 lap: " + Car1Lap);
			}
			else if(Car1Waypoint4 == true &amp;&amp; waypoint.gameObject.name == "WaypointFinal")
			{
			Car1FinalWaypoint = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car1 lap: " + Car1Lap);
			}
		}
		else if(Car1Lap != lapsToMake &amp;&amp; Car1FinalWaypoint == true)
		{
		Car1Lap++;
		print("Lap: " + Car1Lap);
		Car1Waypoint1 = false;
		Car1Waypoint2 = false;
		Car1Waypoint3 = false;
		Car1Waypoint4 = false;
		Car1FinalWaypoint = false;
		}
		else if(Car1Lap == lapsToMake &amp;&amp; Car1FinalWaypoint == true)
		{
		print("The race finished by " + other.attachedRigidbody.name + "!");
		Time.timeScale = 0;
		}
	}
	else
	{
		if(Car2Lap <= lapsToMake &amp;&amp; Car2FinalWaypoint == false)
		{
			if(Car2Waypoint1 == false &amp;&amp; waypoint.gameObject.name == "Waypoint1")
			{
			Car2Waypoint1 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car2 lap: " + Car2Lap);
			}
			else if(Car2Waypoint1 == true &amp;&amp; waypoint.gameObject.name == "Waypoint2" )
			{
			Car2Waypoint2 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car2 lap: " + Car2Lap);
			}
			else if(Car2Waypoint2 == true &amp;&amp; waypoint.gameObject.name == "Waypoint3")
			{
			Car2Waypoint3 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car2 lap: " + Car2Lap);
			}
			else if(Car2Waypoint3 == true &amp;&amp; waypoint.gameObject.name == "Waypoint4")
			{
			Car2Waypoint4 = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car2 lap: " + Car2Lap);
			}
			else if(Car2Waypoint4 == true &amp;&amp; waypoint.gameObject.name == "WaypointFinal")
			{
			Car2FinalWaypoint = true;
			print(other.attachedRigidbody.name + " || " + waypoint.gameObject.name + " || Car2 lap: " + Car2Lap);
			}
		}
		else if(Car2Lap != lapsToMake &amp;&amp; Car2FinalWaypoint == true)
		{
		Car2Lap++;
		print("Lap: " + Car2Lap);
		Car2Waypoint1 = false;
		Car2Waypoint2 = false;
		Car2Waypoint3 = false;
		Car2Waypoint4 = false;
		Car2FinalWaypoint = false;
		}
		else if(Car2Lap == lapsToMake &amp;&amp; Car2FinalWaypoint == true)
		{
		print("The race finished by " + other.attachedRigidbody.name + "!");
		Time.timeScale = 0;
		}
	}
}

(Replace & amp; word with "&" itself, as the code tag doesn't support displaying ampersands.)

That how it looks in the script itself. As stated earlier, these horrible variables will be replaced with more elegant arrays, and print functions removed (and preferably some GUI code of checkpoints reached put in their place). ;-)

Any ideas to improve it? This system is a very simple one, doesn't trace any movement on the track itself, can't calculate distance to the finish line, etc.

Comments
portounity
portounity

Try thi one it works , just need some bugs fix. that i realy donĀ“t know how to.
www//.gotow.net (Remove // to go to the site)
i use for my project i just waiting for some one can figure out the problem.

The problem is when the car A.I crash they some times start to drive backwards
how to fix this?
Need to fix the A.I when they crash , the A.I return to drive in correct position. when they stuck, return to race etc... Like Destruction derby. that is my Game in progress.

Reply Good karma Bad karma+2 votes
feillyne Author
feillyne

Well, to be completely honest with you, I haven't coded in any AI behaviours so far, so can't help you with such little experience.

Reply Good karma+1 vote
Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.