- Home /
How to create a digital Speedometer.
Hey Guys! I was wondering how would i go about scripting a on screen speedometer. atm im using
var mphDisplay : GUIText;
var mph = rigidbody.velocity.magnitude * 2.237; mphDisplay.text = mph + " MPH";
This works fine. But its not really the way i would like to see it. I would prefer to see it as like a bunch of Colorful bars on the button right of the screen. And when i reach higher speeds the lights will increase as it gets to the top speed it be red. I know this sounds confusing and it is but does anyone know of a tutorial or something that could end in result of having a awesome speedometer. thank you.
Is this C# friendly? Every time I put in
var mphDisplay : GUIText;
I get errors because of the ":". A bit new to scripting, but hoping to stay in C#. How can I address that?
Answer by aldonaletto · May 30, 2012 at 12:50 PM
If you want something like a led VU, use GUI.DrawTexture to draw the bars proportionally to the speed:
var barTexture: Texture2D; // select some white or gray texture var barWidth: float = 5; var barHeight: float = 40; var barSpace: float = 7;
function OnGUI(){ // set the speedometer top-left corner var pos = Vector2(Screen.width - 90, Screen.height - 50); // get the speed in mph: var mph = rigidbody.velocity.magnitude * 2.237; // draw the bars: each bar means 5 mph, no bar if speed < 1: for (var v: float = 1; v NOTE: If you want to show the speed numerically too, set the format to avoid tons of digits:
// display the speed with a single decimal digit:
mphDisplay.text = mph.ToString("F1") + " MPH";
@Aldo Naletto, $$anonymous$$ate... You have done it again... You sir are brilliant. This is exactly what i wanted thank you mate. You sir deserve a medal. Thanks!!!!! :)
edit, Is there anyway to make the colors a little brighter?
The colors Color.green, Color.red etc. are as bright as they could be. I suppose you should try a suitable texture in barTexture to get a "glowing appearance" - maybe the image of a rectangular bright led. Remember to convert the texture to black-and-white - this ensures that all RGB color components have the same intensity in any pixel, allowing GUI.color to control the final color.
very good but $$anonymous$$e only seems to do falling speed not driving speed
if you want just the horizontal speed, zero its Y component:
var vel: Vector3 = rigidbody.velocity;
vel.y = 0;
var mph = vel.magnitude * 2.237;
Answer by tomka · May 30, 2012 at 07:21 AM
Good answer provided here: http://answers.unity3d.com/questions/15414/how-can-i-make-an-on-screen-speedometer.html
Prefab: http://forum.unity3d.com/threads/63885-Car-speedometer-dial
Asset store: http://yusufakdag.com/index.php/2011/09/22/speedometer-ui-for-unity3d/
Top 3 links in google for "unity 3d speedometer" in Google ;) https://www.google.com.au/search?aq=f&sugexp=chrome,mod=7&sourceid=chrome&ie=UTF-8&q=unity+3d+speedometer
Thanks for your reply. I have followed that tut and got it working. But its not what im after... Guess i just gotta keep learning untill i can figure this one out for myself. Thank you though