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 DarkFire1 · Aug 30, 2014 at 03:08 AM · rigidbodypositiondistancedisplayracing

Odometer for a racing game

Hello! I'm working on a racing game with a long distance explore/campain mode. I have a speedometer with a working needle, but I think it will add a touch of realism to have a GUIText odometer underneath it. I have a GUIText object created, but no code to go with it.

My car does have a rigidbody attached. I'm not sure if I can use rigidbody.position or a similar method to display the mileage in a GUIText form.

I'm sorry that I don't have any sample code, being relatively new to racing games! Thank you in advance for any answers/comments/suggestions you may have!

Edit: I'm sorry for referring to the odometer as "kilometer" before and for any confusion it may have caused!

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
Best Answer

Answer by rutter · Aug 30, 2014 at 05:04 AM

Well, is the car traveling in a straight line? If so, you just need to calculate the distance between your starting location and wherever you're at, now.

 private var startPosition : Vector3;
 
 function Start() {
     startPosition = transform.position;
 }
 
 function Update() {
     float distance = Vector3.Distance(startPosition, transform.position);
 }

On the other hand, what if your car's path is twisting and turning constantly? Well, in that case you could estimate the distance it travels during each frame.

 private var totalDistance : float;
 private var lastPosition : Vector3;
 
 function Start() {
     lastPosition = transform.position;
 }
 
 function Update() {
     float frameDistance = Vector3.Distance(lastPosition, transform.position);
     totalDistance += frameDistance;
     lastPosition = transform.position;
 }
Comment
Add comment · Show 1 · 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 DarkFire1 · Aug 30, 2014 at 04:10 PM 0
Share

Thank you! I made some $$anonymous$$or adjustments to the script to display the value as text. I'll put them here in case any other Unity users run into the same problem in the future. Simply add:

     `public var odometer :GUIText;


//Put in the Update function to display the distance accurately each frame.

  odometer.text = totalDistance + " $$anonymous$$eters Traveled";` 

   

Another line can be added to convert meters into miles/kilometers and display it smoothly.

Thank you for solving this problem and for your prompt answer!

avatar image
0

Answer by Kiwasi · Aug 30, 2014 at 05:14 AM

Another option is to integrate velocity each frame.

 TotalDistance += rigidbody.velocity.magnitude * Time.deltaTime;
Comment
Add comment · Show 1 · 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 DarkFire1 · Aug 30, 2014 at 04:11 PM 0
Share

Thank you for your suggestion as well!

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

Can I arbitrarily move a dynamic Rigidbody 2D? 1 Answer

Best way to find triangle that a given point occupies. 1 Answer

calculate the estimated distance travel based on the Force applied to Rigid Body 1 Answer

Distance along one axis? 1 Answer

Display countdown timer in Unity: distance traveled C# 1 Answer


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