• 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 /
  • Help Room /
avatar image
0
Question by ekristopher93 · Nov 19, 2019 at 09:32 PM · uitextdisplayspeedometer

I Need Help Making A Speedometer Using UI Text.

I Need Help Making A Speedometer That Is Displayed On The UI Canvas While Using A Text Box As The Speedometer. I Would Like It To Display The current_speed In The Text Box Where It Says 1000. Is That Possible By Any Chance? If Not, Are There Other Alternatives?

Here's A Line Of Code I Used For My Spaceship Racer:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Control : MonoBehaviour {

 /*Ship handling parameters*/
 public float fwd_accel = 100f;
 public float fwd_max_speed = 200f;
 public float brake_speed = 200f;
 public float turn_speed = 50f;

 /*Auto adjust to track surface parameters*/
 public float hover_height = 3f;     //Distance to keep from the ground
 public float height_smooth = 10f;   //How fast the ship will readjust to "hover_height"
 public float pitch_smooth = 5f;     //How fast the ship will adjust its rotation to match track normal

 /*We will use all this stuff later*/
 private Vector3 prev_up;
 public float yaw;
 private float smooth_y;
 private float current_speed;


 void Update()
 {
     /*Here we get user input to calculate the speed the ship will get*/
     if (Input.GetKey(KeyCode.JoystickButton0))
     {
         /*Increase our current speed only if it is not greater than fwd_max_speed*/
         current_speed += (current_speed >= fwd_max_speed) ? 0f : fwd_accel * Time.deltaTime;
     }
     else
     {
         if (current_speed > 0)
         {
             /*The ship will slow down by itself if we dont accelerate*/
             current_speed -= brake_speed * Time.deltaTime;
         }
         else
         {
             current_speed = 0f;
         }
     }

     /*We get the user input and modifiy the direction the ship will face towards*/
     yaw += turn_speed * Time.deltaTime * Input.GetAxis("Horizontal");
     /*We want to save our current transform.up vector so we can smoothly change it later*/
     prev_up = transform.up;
     /*Now we set all angles to zero except for the Y which corresponds to the Yaw*/
     transform.rotation = Quaternion.Euler(0, yaw, 0);

     RaycastHit hit;
     if (Physics.Raycast(transform.position, -prev_up, out hit))
     {
         Debug.DrawLine(transform.position, hit.point);

         /*Here are the meat and potatoes: first we calculate the new up vector for the ship using lerp so that it is smoothed*/
         Vector3 desired_up = Vector3.Lerp(prev_up, hit.normal, Time.deltaTime * pitch_smooth);
         /*Then we get the angle that we have to rotate in quaternion format*/
         Quaternion tilt = Quaternion.FromToRotation(transform.up, desired_up);
         /*Now we apply it to the ship with the quaternion product property*/
         transform.rotation = tilt * transform.rotation;

         /*Smoothly adjust our height*/
         smooth_y = Mathf.Lerp(smooth_y, hover_height - hit.distance, Time.deltaTime * height_smooth);
         transform.localPosition += prev_up * smooth_y;
     }

     /*Finally we move the ship forward according to the speed we calculated before*/
     transform.position += transform.forward * (current_speed * Time.deltaTime);
 }

}alt text

unity-help.png (4.2 kB)
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

275 People are following this question.

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

Related Questions

BCE0022: Cannot convert 'String' to 'int' 0 Answers

Reserve ammo not displaying on UI 0 Answers

Can not properly display the score on pong game 0 Answers

MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. 5 Answers

how do i change max size value of text component unity? 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