|
I have everything on my car but the speedometer and i just cant figure it out how to script one. so is can anyone help me out by making one? my game objects is "Car" and i just want to apply the script to my dial.
(comments are locked)
|
|
The answer to this largely depends whether you want to display a numerical readout, or as a rotating needle. First, to get the actual speed value, as mentioned in another answer, you can simply use the rigidbody's velocity. However, Unity's default scale is one unit = one meter, so reading To convert to MPH, you can use:
or to convert to KPH:
Next to display it. To show the speed as a numerical readout is comparitively simple: 1) Create a GUIText gameobject (from the gameobject menu). 2) In your car script, create a var which will store a reference to this GUIText GameObject:
3) Select your car, and drag a reference from the new GUIText GameObject in the hierarchy into this variable slot in car script, in the inspector. 4) Now, in your car script, you can add the lines in your Update() function to calculate the MPH, and update the text displayed:
That should get you working with a numerical readout. To display as a rotating needle requires some trickier coordination. There's no simple way to rotate a GUI Texture, or a texture drawn using the OnGUI method, so I've written a small general-purpose script which you can place on a gameobject to create a rotatable GUI Texture. You can control it by setting the 'angle' variable from other scripts. So: 1) Create a new C# script. Name it "RotatableGuiItem", and paste in this script:
3) Create a new empty GameObject. Name it "mph needle". Add the RotatableGuiItem script to it. 4) Assign your speedo needle "Texture" variable. You probably want to use a texture with a transparent alpha background for this, and bear in mind that it will rotate around the centre of the image. Adjust the "size" values to match the size of your texture. 5) Adjust the position of the texture using the X and Y position values of the GameObject in the inspector, so that it is your desired position. (probably in the centre of a normal static GUI Texture showing the mph dial face). 6) In your car script, create a var which will store a reference to this rotatable GUI item:
7) Select your car, and drag a reference from the "mph needle" GameObject in the hierarchy into this variable slot in car script, in the inspector. 8) Now, in your car script, you can add the lines in your Update() function to calculate the MPH, and update the needle's angle:
You will probably need to adjust how far the needle turns in relation to the mph, and at what angle it starts, so you may end up with a line which looks more like this:
Which means the needle will be rotated by 20 degrees when the mph is zero, and will rotate 1.4 degrees for every 1 mph. (If you want to control this script from a Javascript script, you'll have to move the C# RotatableGuiItem into a folder called PlugIns in your assets.) Hopefully this should get you to the stage where you have a working speedometer with a rotating needle! thanks for the help the second one crashes unity but i can use the first one
Apr 19 '10 at 12:08 AM
dontay
It crashes unity?! weird. As in, it makes unity quit out? or an error? or it hangs and becomes unresponsive? Please give more detail - it shouldn't crash. It works for me.
Apr 19 '10 at 09:06 AM
duck ♦♦
thanks for this dude, awesome .
Apr 19 '10 at 05:43 PM
Fishman92
can I take it that it doesn't crash for you, fishman? :)
Apr 19 '10 at 05:52 PM
duck ♦♦
this really helped me out too, thanks ;)
Nov 22 '10 at 01:35 PM
Yoerick
(comments are locked)
|
|
How have you made the car move? If you've used the wheel collider there's a value called rpm (revolutions per minute), you can use this plus the radius of the wheel to get an accurate mph for your speed dial. Using these formulai: circumference = 2*pi*r 1km = 0.621371192 miles So if your wheel has 0.3 meters radius and you have an rpm of 100 you'll be going at: 2*3.14*0.3*100*60 = 11,309.7336 meters per hour or 11kmph or 6.8mph If you're using something else then you can get the magnitude of velocity from the rigid body with this:
But this isn't how fast the wheel is spinning so if you're flying through the air it'll not be an accurate display for the speed dial. lol spinaljack... that one is good for a tachometer... my vehicle tends to spend a good amount of time aeroplaning(not hydro...) correct me if I am wrong, but your description reads backwards?! When my vehicle is upsidedown with wheels-a-spinnin, it would have 0 velocity, and ultrahigh rmp, lol [laughing jovially, not in a bad way]
Dec 06 '10 at 01:09 AM
dingben
(comments are locked)
|
|
What about if am using the First person control as a game object, and I want a speedometer to appear on the screen. How do I go by it?
(comments are locked)
|
|
very easy to use, i now have a visual speedo in the Unity Car Tutorial. thank you very much
(comments are locked)
|
