Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ourchat · Aug 01, 2011 at 09:33 PM · carrace

How do I make laps in a racing game?

Anyone please help me to created easy LAP system for my Racing Game... You are MASTER if can help me... :D

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Aug 01, 2011 at 09:59 PM 3
Share

Unity Answers is not a script writing request service.

avatar image save · Aug 02, 2011 at 12:35 AM 0
Share

I took the liberty to change the title of your question so people would find the solution more easily.

avatar image Moomooz · Jul 20, 2012 at 06:27 PM 0
Share

Can anyone tell me how I can attach these two scripts to the Unity car tutorial please ?

5 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by save · Aug 02, 2011 at 12:31 AM

This question gets asked now and then, so I thought it would be good to once and for all show an easy example of how checkpoints and laps can be achieved. In a few simple steps you can count laps around a course with the help of triggers. This is one way to skin the cat, or crack the nut if you will:

First of all we need an object that will trigger the checkpoint areas, a player for instance. To the player we add the ability to keep track of our current checkpoint number, the current lap and storage of all checkpoints as GameObjects in an array.

Player

 #pragma strict
 
 var checkPointArray : Transform[]; //Checkpoint GameObjects stored as an array
 static var currentCheckpoint : int = 0; //Current checkpoint
 static var currentLap : int = 0; //Current lap
 static var startPos : Vector3; //Starting position
 private var moveMultiplier : int = 8; //Multiplies the Input.GetAxis movement of our player
 
 function Start () {
     //Set a simple visual aid for the Checkpoints
     for (objAlpha in checkPointArray) {
         objAlpha.renderer.material.color.a = 0.2;
     }
     checkPointArray[0].renderer.material.color.a = 0.8;
     
     //Store the starting position of the player
     startPos = transform.position;
 }
 
 function FixedUpdate () {
     //Movement for the player (Standard Input: Arrow Keys/W,A,S,D)
     rigidbody.AddForce(Input.GetAxis("Horizontal")*moveMultiplier, 0, Input.GetAxis("Vertical")*moveMultiplier);
 }

The checkpoints will be triggered if it's the correct checkpoint according to the currentCheckpoint variable (which the player keeps track of). If that variable exceeds our current available number of checkpoints we add to number of laps and reset the currentCheckpoint variable.

Checkpoints

 #pragma strict
 
 static var playerTransform : Transform; //Store the player transform
 
 function Start () {
     playerTransform = gameObject.Find("Sphere (Player)").transform; //Set the player transform
 }
 
 function OnTriggerEnter (other : Collider) {
     //Is it the Player who enters the collider?
     if (!other.CompareTag("Player")) 
         return; //If it's not the player dont continue
         
     //Is this transform equal to the transform of checkpointArrays[currentCheckpoint]?
     if (transform==playerTransform.GetComponent(player).checkPointArray[player.currentCheckpoint].transform) {
         //Check so we dont exceed our checkpoint quantity
         if (player.currentCheckpoint+1<playerTransform.GetComponent(player).checkPointArray.length) {
             //Add to currentLap if currentCheckpoint is 0
             if(player.currentCheckpoint==0)
                 player.currentLap++;
             player.currentCheckpoint++;
         } else {
             //If we dont have any Checkpoints left, go back to 0
             player.currentCheckpoint=0;
         }
         visualAid(); //Run a coroutine to update the visual aid of our Checkpoints
         //Update the 3dtext
         Camera.main.GetComponentInChildren(TextMesh).text = "Checkpoint: "+(player.currentCheckpoint)+" Lap: "+(player.currentLap);
     }
 }
 
 function visualAid () {
     //Set a simple visual aid for the Checkpoints
     for (objAlpha in playerTransform.GetComponent(player).checkPointArray) {
         objAlpha.renderer.material.color.a = 0.2;
     }
     playerTransform.GetComponent(player).checkPointArray[player.currentCheckpoint].renderer.material.color.a = 0.8;
 }


See online demo

Download the source

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CHPedersen · Aug 02, 2011 at 06:50 AM 0
Share

Your zeal in this regard is admirable, dude. I'd plus it more than 1 if I could. I can't help it but fear you're fighting an uphill battle, though...

avatar image biohazard · Aug 02, 2011 at 07:01 AM 0
Share

If i may add, it could also be solved via triggers and iterators :)

just a guess of course^^

also, great answer as usual :)

avatar image save · Aug 02, 2011 at 07:42 AM 0
Share

@biohazard That's right, this is sort of an iteration, but I see your point of method. @Christian-H-Pedersen "When a day starts like this it's all uphill from here". :-)

avatar image ourchat · Aug 03, 2011 at 08:15 AM 0
Share

Thanks for your answer my $$anonymous$$aster...my game is completed now....

avatar image save · Aug 03, 2011 at 08:18 AM 0
Share

Good thing @ourchat. You could mark the above answer as correct to make people know that this has been solved (it will also give you karma closing up questions). @$$anonymous$$eltdown is also pointing to a good solution.

Show more comments
avatar image
1

Answer by Meltdown · Aug 02, 2011 at 05:17 PM

Check out Step #5 of this excellent car introductory tutorial. There is a good summary on implementing waypoints and getting AI cars to follow them.

Although for more robust AI you would want to look at implementing UnitySteer.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by GluedBrain · May 05, 2013 at 10:02 AM

LAPS AND CHECKPOINT SYSTEM

You Might want to check this

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Nomibuilder · Apr 08, 2015 at 07:16 AM

If you are using C# Language. Then here is the C# version of CheckPoint and Laps system.

Laps Script

 using UnityEngine;
 using System.Collections;
 
 public class Laps : MonoBehaviour {
     
     // These Static Variables are accessed in "checkpoint" Script
     public Transform[] checkPointArray;
     public static Transform[] checkpointA;
     public static int currentCheckpoint = 0; 
     public static int currentLap = 0; 
     public Vector3 startPos;
     public int Lap;
     
     void  Start ()
     {
         startPos = transform.position;
         currentCheckpoint = 0;
         currentLap = 0; 
 
     }
 
     void  Update ()
     {
         Lap = currentLap;
         checkpointA = checkPointArray;
     }
     
 }


Check Point Script

 using UnityEngine;
 using System.Collections;
 
 public class checkpoint : MonoBehaviour {
     
     void  Start ()
     {
 
     }
     
     void  OnTriggerEnter ( Collider other  )
     {
         //Is it the Player who enters the collider?
         if (!other.CompareTag("Player")) 
             return; //If it's not the player dont continue
         
 
         if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform) 
         {
             //Check so we dont exceed our checkpoint quantity
             if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length) 
             {
                 //Add to currentLap if currentCheckpoint is 0
                 if(Laps.currentCheckpoint == 0)
                     Laps.currentLap++;
                 Laps.currentCheckpoint++;
             } 
             else 
             {
                 //If we dont have any Checkpoints left, go back to 0
                 Laps.currentCheckpoint = 0;
             }
         }
 
 
     }
 
 }


And Just Follow this tutorial to attach scripts on your Game Objects. Link

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Maulik2208 · Jan 02, 2013 at 05:03 AM

Go to youtube and find for the same you will surely get it with demo enjoy.....and for your kind information this question is massively repeated question on unity answer so you can also check that as well.....cheers enjoy.....

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Race checkpoint system small help please :) 1 Answer

Racing Game - setting up a car TIRES 1 Answer

How to build a lap system for racing game 4 Answers

Show laptime after each lap ? 1 Answer

Best Packages to Make Online Car Race Game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges