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));			  
	}  
}

1 Answer

1

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));              
}  

@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.

–