Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 james_smut · Apr 21, 2012 at 12:45 AM · triggertimercounter

Lap timer using colliders

Hi everyone, I hope you can help me with a problem I have been having, I am trying to include a lap timer that restarts after each lap (pretty normal) using the code that I already have, I need it to simply start when it passes the first collider/sector and finish when it reaches that same one again (i.e. one full lap).

I looked up how to set up the track and got my answer from here too, my track is set up by creating 3 trigger boxes around the track, the code then checks to see the triggers had been gone through in order, once this is done the next lap begins.

this is my sectors script which checks all the sectors have been navigated (the script is placed onto all of my checkpoints/sectors

 static var playerTransform : Transform;


function Start () {

 playerTransform = gameObject.Find("Nissan_GTR").transform;

}

function OnTriggerExit (other : Collider) {

 //check if this transform equals current checkpoint transform

 if (transform == playerTransform.GetComponent(Car).sectorArray[Car.currentSector].transform) {

     //do not exceed checkpoint quantity

     if (Car.currentSector + 1 < playerTransform.GetComponent(Car).sectorArray.length) {

         //add lap if completed all checkpoints

         if(Car.currentSector == 0){

             Car.currentLap++;

         }

         Car.currentSector++;

     }

     else {

         //no checkpoints remain, set to 0

         Car.currentSector = 0;

     }

     Camera.main.GetComponentInChildren(TextMesh).text = " Lap " + (Car.currentLap);

 }

}

This is my one of my car scripts that is placed onto the car

 var sectorArray : Transform[]; //Checkpoint GameObjects stored as an array

private static public var currentSector : int = 0; //Current checkpoint

private static public var currentLap : int = 0; //Current lap

private static public var startPos : Vector3; //Starting position

function Start(){

 startPos = transform.position;

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Atrius · Apr 21, 2012 at 03:17 AM

This seems overly complex, but I could be missing something. You should be able to use OnTriggerEnter() on your sections to detect what sector the car is in. Use other.gameObject to reference the car that is colliding with your trigger.

Then if your car script was called LapStats for example you could do:

 var lapScript : LapStats = other.gameObject.GetComponent("LapStats");
 if (lapScript) {
     lapScript.currentSector = sector_number;
 }

If it's the final sector of the track you could use the OnTriggerExit to increment the lap counter in the same way.

If you need to track each checkpoint because a person can miss one, then you could simply do a check to confirm that the sector is in sequence, ie. sector_number - 1 = currentSector. Without having to track a variable for each sector per lap. Just make sure they go in sequence.

Just some thoughts, I could be misinterpreting what you are doing.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change main Camera to another camera with a Trigger then back with a Timer. 1 Answer

Can't make time go down after picking up a coin, 2 Answers

Help with making a triggered gui message 1 Answer

Stop watch for 100 meter dash 1 Answer

Event to be triggered after constant contact with gameobject 2 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