Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
7
Question by dontay · Apr 18, 2010 at 06:38 PM · graphicscarspeedometerdial

How can I make an on-screen speedometer?

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.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
20
Best Answer

Answer by duck · Apr 18, 2010 at 09:06 PM

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 rigidbody.velocity.magnitude will give you the speed in meters per second.

To convert to MPH, you can use:

var mph = rigidbody.velocity.magnitude * 2.237;

or to convert to KPH:

var kph = rigidbody.velocity.magnitude * 3.6;


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:

var mphDisplay : GUIText;

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:

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

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:

using UnityEngine; [ExecuteInEditMode()] public class RotatableGuiItem : MonoBehaviour {

 public Texture2D texture = null;
 public float angle = 0;
 public Vector2 size = new Vector2(128, 128);
 Vector2 pos = new Vector2(0, 0);
 Rect rect;
 Vector2 pivot;

 void Start() {
     UpdateSettings();
 }

 void UpdateSettings() {
     pos = new Vector2(transform.localPosition.x, transform.localPosition.y);
     rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y);
     pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
 }

 void OnGUI() {
     if (Application.isEditor) { UpdateSettings(); }
     Matrix4x4 matrixBackup = GUI.matrix;
     GUIUtility.RotateAroundPivot(angle, pivot);
     GUI.DrawTexture(rect, texture);
     GUI.matrix = matrixBackup;
 }

}

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:

var mphNeedle : RotatableGuiItem;

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:

var mph = rigidbody.velocity.magnitude * 2.237;
mphNeedle.angle = mph;

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:

mphNeedle.angle = 20 + mph * 1.4f;

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!

Comment
Add comment · Show 10 · 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 dontay · Apr 19, 2010 at 12:08 AM 0
Share

thanks for the help the second one crashes unity but i can use the first one

avatar image duck ♦♦ · Apr 19, 2010 at 09:06 AM 0
Share

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.

avatar image HolBol · Apr 19, 2010 at 05:43 PM 0
Share

thanks for this dude, awesome .

avatar image duck ♦♦ · Apr 19, 2010 at 05:52 PM 0
Share

can I take it that it doesn't crash for you, fishman? :)

avatar image Yoerick · Nov 22, 2010 at 01:35 PM 0
Share

this really helped me out too, thanks ;)

Show more comments
avatar image
1

Answer by spinaljack · Apr 18, 2010 at 08:23 PM

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:

var speed = rigidbody.velocity.magnitude;

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.

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 dingben · Dec 06, 2010 at 01:09 AM 0
Share

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]

avatar image
1

Answer by Nomibuilder · Dec 02, 2014 at 03:38 PM

You may use this simple code in your Car Control Script.

 public var Speed : float;
 public var maxSpeed : float = 150;
 
 var speedOMeterDial : Texture2D;    //GUI Texture for dial
 var speedOMeterPointer : Texture2D;    
 
 var minAnglePointer : int = -90;
 var maxAnglePointer : int = 180;
 
 function Update () 
 {
     Speed = rigidbody.velocity.magnitude * 3.6f;
         if(Speed > maxSpeed)
     {
         Wheel_FL.motorTorque = 0;
         Wheel_FR.motorTorque = 0;
     }
 else
     {
 Wheel_FL.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
 Wheel_FR.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
     }
 }
 
 function OnGUI (){
     GUI.DrawTexture(Rect(Screen.width - 300,Screen.height - 300,300,300),speedOMeterDial);
     var speedFactor : float = Speed / maxSpeed;
     var rotationAngle : float;
     if (Speed >= 0)
         {
           rotationAngle = Mathf.Lerp(minAnglePointer,maxAnglePointer,speedFactor);
         }
           else 
         {
           rotationAngle = Mathf.Lerp(minAnglePointer,maxAnglePointer,-speedFactor);
         }
     GUIUtility.RotateAroundPivot(rotationAngle,Vector2(Screen.width - 150 ,Screen.height - 150));
     GUI.DrawTexture(Rect(Screen.width - 300,Screen.height - 300,300,300),speedOMeterPointer);
 
 }
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 ChrisIrwin · Dec 11, 2015 at 08:10 AM 1
Share

Can you translate this for C#?

avatar image Nomibuilder ChrisIrwin · Dec 11, 2015 at 07:32 PM 0
Share

using UnityEngine; using System.Collections;

public class Speed$$anonymous$$eter : $$anonymous$$onoBehaviour { public float Speed; public float maxSpeed = 150;

public Texture2D speedO$$anonymous$$eterDial; //GUI Texture for dial public Texture2D speedO$$anonymous$$eterPointer;

int $$anonymous$$AnglePointer = -90; int maxAnglePointer = 180;

void Update () { Speed = rigidbody.velocity.magnitude 3.6ff; if(Speed > maxSpeed) { Wheel_FL.motorTorque = 0; Wheel_FR.motorTorque = 0; } else { Wheel_FL.motorTorque = EngineTorque / GearRatio[CurrentGear] Input.GetAxis("Vertical"); Wheel_FR.motorTorque = EngineTorque / GearRatio[CurrentGear] *Input.GetAxis("Vertical"); } }

void OnGUI (){ GUI.DrawTexture( new Rect(Screen.width - 300,Screen.height - 300,300,300),speedO$$anonymous$$eterDial); float speedFactor = Speed / maxSpeed; float rotationAngle; if (Speed >= 0) { rotationAngle = $$anonymous$$athf.Lerp($$anonymous$$AnglePointer,maxAnglePointer,speedFactor); } else { rotationAngle = $$anonymous$$athf.Lerp($$anonymous$$AnglePointer,maxAnglePointer,-speedFactor); } GUIUtility.RotateAroundPivot(rotationAngle,Vector2(Screen.width - 150 ,Screen.height - 150)); GUI.DrawTexture( new Rect(Screen.width - 300,Screen.height - 300,300,300),speedO$$anonymous$$eterPointer);

} }

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

8 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Car speedometer MPH 1 Answer

Automatic Car Moving Script 1 Answer

Speedometer Error. PLS help ((( 0 Answers

Assigning imported mesh from script 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