• 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
0
Question by IBDelta · Aug 25, 2011 at 11:55 PM · c#speedometer

3D Tachometer scripting C#

Here's one for someone with a better head for maths than myself. I'm trying to make a simple 3D needle dial for use in speedometers, among other things. I would like to make this as flexible and user definable as possible as I intend to make it available for download when complete. I apologise if it appears that I'm trying to get other people to do my coding for me but this sort of number juggling is a little beyond me at the moment.

 using UnityEngine;  
 using System.Collections;
 
 public class Airspeed : MonoBehaviour {
     
 public Transform needle;//The needle object of the meter
     
 public float inputValue=0f;// The value provided by the player  
 public float inputMin=0f;// The minimum expected value for the meter's lowest reading  
 public float inputMax=0f;// The maximum expected value for the meter's highest reading  
 float inputTemp=0f;    //Temporary number
     
 float currentReading=0f; // The angle of rotation for the needle object  
 public float minReading=0f;// The minimum angle of rotation for the needle object  
 public float maxReading=0f;// The maximum angle of rotation for the needle object  
     
 
     void FixedUpdate()
     {
         //The following line attempts to turn the inputValue into a 0f to 10f scale  
         //It doesn't work :-(  
         inputTemp=((inputMax - inputMin)*inputValue)+inputMin;  
 
         //The following line attempts to find the rotation setting for the needle  
         //using the previous 0f to 10f scale as a percentage of the  
         //min and max rotations allowed for the needle.  
         //It's unlikely that this works as expected.  
         currentReading=((inputTemp/100)*(maxReading - minReading))+minReading;    
 
         //I'm pretty sure this next line is the only bit that does what's expected  
         needle.rotation=Quaternion.Euler(new Vector3(0f,0f,currentReading));              
     }  
 }  
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

1 Reply

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

Answer by aldonaletto · Aug 26, 2011 at 12:29 AM

It's better to convert the input value to the 0..1 range - where inputMin is 0 and inputMax is 1 - then multiply it by the angle range (maxReading-minReading) and add minReading like you were already doing:

void FixedUpdate()
{
    // Convert the inputValue to the range 0f to 1f - it's easier to work this way
    inputTemp = (inputValue - inputMin)/(inputMax - inputMin);  
    currentReading = inputTemp*(maxReading - minReading) + minReading;    
    needle.rotation = Quaternion.Euler(new Vector3(0f,0f,currentReading));              
}  
Comment
Add comment · Show 3 · 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 IBDelta · Aug 26, 2011 at 12:58 AM 0
Share

Tested and working! Greatly appreciated, you are a scholar and a gent sir :-)

avatar image aldonaletto · Aug 26, 2011 at 03:36 AM 0
Share

@IBDelta, if this solved the problem, please click the "accept" button (the check mark below the voting thumbs) in this answer - it helps other people to find both, the question and the answer.

avatar image IBDelta · Aug 26, 2011 at 11:57 AM 0
Share

I missed that one, cheers!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

No movement when animation plays (2d) 1 Answer

Created Animations only play half within Animator 0 Answers


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