Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

convert float numbers to an angle (solved)

Discussion in 'Scripting' started by andyH, Mar 17, 2010.

  1. andyH

    andyH

    Joined:
    Mar 2, 2010
    Posts:
    31
    I want to model some realistic looking 3d analogue guages (the type with a needle like a speedometer).

    I am pre-empting the challenge of feeding floating numeric data to the guage to have the needle rotate around the guage face and show accurately the numeric data being fed to it. I will also be modeling some moving-tape type guages and I expect the method of driving them will be similar..

    Anyone know of any tutorials for this type of scripting? or better still anyone done it recently and willing to share code examples?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Using the SuperLerp function from here, let's say you want the speedometer to have a range of 320 degrees, where the resting position is at 200 degrees, and the input is a float representing kilometers per hour where the range is 0 through 240:

    Code (csharp):
    1. var kph : float;
    2.  
    3. function Update () {
    4.     transform.rotation = Quaternion.Euler(0.0, 0.0,
    5.         200.0 + MathS.SuperLerp(0.0, 320.0, 0.0, 240.0, kph));
    6. }
    That would rotate around the Z axis; might need to change the axis depending on how the gauge is set up.

    You could also do

    Code (csharp):
    1.         MathS.SuperLerp(200.0, 520.0, 0.0, 240.0, kph));
    which would be a tiny bit faster although perhaps not quite as clear.

    --Eric
     
  3. andyH

    andyH

    Joined:
    Mar 2, 2010
    Posts:
    31
    Thanks Eric that looks like exactly what I need... I havent had any experience of super lerp... so this should be fun!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    That's because I made it up. :) It's basically Mathf.Lerp(a, b, Mathf.InverseLerp(c, d, t)), which I found that I used quite a bit, so I made a SuperLerp function that does the same thing, except it's easier to type and runs faster.

    --Eric