Saturday, 20 March 2010

Speed counter

Now that the lap counter was done, I moved next onto the speed counter. My first thoughts were that this would be quite tricky, but then I discovered that the rigidbody attachment for the car comes with a built in velocity component (thank god).

I found a nice tutorial for making a speedometer at:

http://answers.unity3d.com/questions/8120/how-can-i-make-an-on-screen-speedometer

It basically converts the velocity of the rigidbody into a MPH reading. Had to make a few changes though, since the MPH were being printed out as a float and not an integer. To solve this, i added the parseInt() method to the MPH reading to convert it into an integer value. The code is below.

var mph = parseInt(rigidbody.velocity.magnitude * 2.237);
mphDisplay.text = mph + " MPH";

If I have time an the end, I would like to go make and make a needle based speedometer, but is it not the most important thing to impliment at the moment.

No comments:

Post a Comment