Programming Checkpoints

Explained in the Time manager post was the use of checkpoints in the game, as players pass through checks points in time trail mode they are given an amount of extra time and given a reset point in case they crash and need to reset the car.

Before giving the player extra time however, it is vital that the check point ensures the player is travelling in the correct direction. If for example the player travels through the checkpoint backwards they should not be awarded the check point. This check is done through a dot product check.

Once the checkpoint has ensured that the player is traveling in the correct direction, it can access the player’s time manager script and award extra time. As well as accessing the time manager script, the checkpoint script also access the player’s  reset script (used to reset the car after a crash) to save the check point as a reset point.

New-Mind-Map (13)

The raw script is pasted below,

public class CheckPoint_Script : MonoBehaviour
{
 private Vector3 checkPointDirection;

 void Start()
 {
  checkPointDirection = transform.forward;
 }

 void OnTriggerEnter(Collider other)
 {
  //Check the obkects tag
  if(other.transform.tag == "Player")
  {
   //Look for the reset script
   ResetCar resetCar = other.GetComponent<ResetCar>();
   if (resetCar != null)
   {
    //Check the player has not passed through the checkpoint twice
    if (resetCar.LastPassedCheckPoint != this.gameObject)
    {
     //Pass this check point as the last past checkpoint
     resetCar.LastPassedCheckPoint = this.gameObject;
     //Look for a rigid body component
     Rigidbody rigid = other.GetComponent<Rigidbody>();
     if (rigid != null)
     {
      //Check the players velocity against the direction of this checkpoint
      //If the value is above 0, the player is traveling in the correct direction
      if (Vector3.Dot(rigid.velocity, checkPointDirection) > 0)
      {
       //Look for the players time manager script
       Player_TimeManager_Script timemanager = other.GetComponent<Player_TimeManager_Script>();
       if (timemanager != null)
       {
        //Activate the checkpointpassed method
        timemanager.CheckPointPassed();
       }
      }
     }
    }
   }
  }
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: