Set a Min and Max rotation?

Hello, I have a car speedometer needle that I want to rotate when the car goes… How do I make it have a Min and Max rotations?

function NeedleRotate() {
 Needle.transform.rotation = Quaternion.Euler(0,0, Time.deltaTime * -FrontLeftWheel.rpm);
}

Mathf.Clamp?

I’ll research into that. Then I will post my results here. :wink:

EDIT: I’m confused on this one. :face_with_spiral_eyes:

Well, first I would go pick up the Speedometer HUD addon from the Unity Asset Store, which is the easiest option.

But if you insist…

Not tested:

var minAngle : float;
var maxAngle : float;
var maxRPM : int;

function NeedleRotate()
{
   float angle = (1.0f / maxRPM * FrontLeftWheel.rpm) * (maxRotation-minRotation) + minRotation;
   transform.rotation = Quaternion.Euler(0, 0, angle);
}

You can add some hysteresis to dampen the RPM effect at the lower end of the scale and some float noise to make the needle bounce at the upper range to make it more authentic.

Thanks. But it says the float angle line needs a semi colon. I do not know why. I do not see any errors?

There is needing help, and there is being lazy. I’m on the fence over which one you’re being with this.

Change “float” to “var”. I was thinking in C# and writing UnityScript.

I changed it to “var angle : float = …” and it said it had problems with unknown identifiers, even though they are set at the top. :wink:

Nvm. I feel stupid right now. :wink:

I fixed it. But it bogs my FPS down to half! It’s at 24 right now when it was between 50-70 before! Any ideas why? It’s even in fixed update…

I cannot imagine that little bit of code would be bogging the frame rate down that much. How are you calling in to it? And how often?

Also, where are you pulling FrontLeftWheel.rpm from? I assume that is a static on another component?

Fixed Update. No clue how much that is compared to Update? The Wheel.rpm is from my car’s FrontLeftWheelCollider. :wink:

My suggestion is to comment out the two lines of code in the NeedleRotate function and see if that improves your frame rate. And if it does, then comment out each line individually, to see which one is killing your frame rate. From there, we can start making an assessment of what is going wrong.