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 Toy · Feb 22, 2012 at 10:00 PM · guitimerspeedreal-timespeedometer

Having a speed counter

Hi there I making a game where my character runs. I have been searching to learn how to make a counter that would show me my speed in real time( km/h ).

Thanx

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

Answer by save · Feb 22, 2012 at 11:05 PM

I believe you could do something like this for an object that isn't a rigidbody:

#pragma strict

private var initialPosition : Vector3; private var myT : Transform;

function Start () { myT = transform; initialPosition = myT.position; }

function Update () { var magnitude : float = (myT.position - initialPosition).magnitude / Time.deltaTime; initialPosition = myT.position; Debug.Log(magnitude); }

Comment
Add comment · Show 2 · 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 save · Feb 22, 2012 at 11:21 PM 0
Share

Don't know if you see the edited version (doesn't update the answer for me), you should be able to simply use:

 #pragma strict
 
 private var initialPosition : Vector3;
 private var myT : Transform;
 
 function Start () {
     myT = transform;
     initialPosition = myT.position;
 }
 
 function Update () {
     var magnitude : float = (myT.position - initialPosition).magnitude / Time.deltaTime;
     initialPosition = myT.position;
     Debug.Log(magnitude);
 }
avatar image Toy · Feb 23, 2012 at 12:46 AM 0
Share

I will try this thank you very much

avatar image
1

Answer by Berenger · Feb 22, 2012 at 10:27 PM

You have two kind of speeds, rigidbody.velocity which (the magnitude) in unit / physic deltaTime, or how much will physics move the object during the next physic loop, and the one you have to calculate yourself, (lastPos-currPos).magnituge, which is unit / regular deltaTime if done in Update and unit / physic deltaTime from FixedUpdate. That's the theory.

Now, what you want to display will probably be

float speed; // Update is called once per frame void FixedUpdate () { speed = rigidbody.velocity.magnitude; // Unit / physic dt speed *= Time.fixedDeltaTime; // Unit / s, fixedDeltaTime being 1 / physic dt }

void OnGUI() { GUI.Label( new Rect(10, 10, 300, 100 ), speed + " Unit/s" ); GUI.Label( new Rect(10, 30, 300, 100 ), (speed 3600) + " Unit/h" ); GUI.Label( new Rect(10, 50, 300, 100 ), (speed 3600 / 1000) + " KUnit/h" ); }

Now, it's up to you to consider one Unit to be a meter or not. By the way, you might want to use coroutine to get an average value for that, just to smooth it out.

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

7 People are following this question.

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

Related Questions

Timer Pause 1 Answer

Problem with speed boost 2 Answers

Marquee.cs speeds up scrolling with keydown 1 Answer

hide gui.label after an event 1 Answer

Car speedometer MPH 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