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
1
Question by Blackelfwolf · Nov 22, 2010 at 01:08 AM · timerracingscoring

How can I make a "Fastest Lap" scoring system for a racing game?

I am making a racing game and need a scoring system. I want it to count the time that it took to do a lap and that you get more points if you do the lap faster (the faster you finish the lap the more points you get). So how do I script or make it?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jesus_Freak · Nov 22, 2010 at 01:19 AM

all right! make a cube, turn it into a trigger (check "isTrigger") and tag it "FinalLap" you'll add this script to your car or whatever you'r racing. and turn off the mesh renderer.

var points = 0;

function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "FinalLap") { Finish(); } }

function Update() { print("I finished in: " +points); } function Finish() { if(Time.time <= 90) //if you make it before 90 seconds { points += 5; //add points } if(Time.time <= 50) //if you made it before 50 seconds { points += 10; //add points } }

that should add points based on the time it took to hit the trigger (box) and prints in the Debug.Log that you finished in a predefined time.

hope this helps!!!

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 duck · Nov 22, 2010 at 01:54 PM

The way I implement this is to have a series of waypoint positions around the track which are collected together as an array of Vector3s. (See here for more detail on that)

I then track each waypoint that the user passes using a simple "Dot" vector function. Because I know how many waypoints there are, I can track when the user has passed the last one, and begin checking for the first waypoint again. Whenever the first waypoint is passed, I update the lap counter, and record the lapTime.

This method also gives you easy access to how far around the track a user is (eg, if they've passed 45 of 150 waypoints, they have completed ((45/150)*100)% of the lap!

int lapNum = 1; int waypointNum = 0; Vector3[] waypoints; // this should be filled with vector3 world positions as show in answer linked above in this question

void Update() { // we get the current target waypoint and the next waypoint // (in order to calculate the waypoint's direction) waypoint = waypoints[waypointNum]; nextwaypoint = waypoints[ (waypointNum+1) % waypoints.Length ];

 // route direction at target waypoint:
 waypointDirection = nextwaypoint - waypoint;

 // delta from car to target waypoint
 Vector3 waypointDelta = wayPoint-transform.position;

 // check whether car has passed target waypoint, using Dot Product
 if (Vector3.Dot( wayPointDirection, waypointDelta ) &lt; 0)
 {
     // if so, step to the next waypoint number.
     waypointNum++;

     // check whether we passed the last one
     if (waypointNum == waypoints.Length)
     {
         // we completed a lap. record time and reset waypoint tracking to zero.
         lapTime = (Time.time - lapStartTime);
         if (lapTime &lt; fastestLapTime) {
           // new fastest lap!
         }
         waypointNum = 0;
         ++lapNum;
         lapStartTime = Time.time;
     }
 }

}

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity Game Score Script 1 Answer

SHMUP - Scoring and Scripting Help 1 Answer

saving scores and let other user to view it 2 Answers

Lap timer not updating best lap milliseconds...not efficient enough? 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