Set rotation based on speed ratio

I’m trying to create a speedometer for my car, however I’m stumped on how to figure out the rotation based on speed.

This is kinda what I got so far. MinAngle is the rotation of the needle at 0 speed. MaxAngle is the rotation of the needle at top speed (which is 200, MaxSpeed)

TotalArc is the sum of those 2 together. Not sure if that’s needed, but figured if I could do some sort of equation that’d get like speed / totalArc or something that I could then get the rotation figured out, but I’m not too sure

local MinAngle = -100
local MaxAngle = 100

local TotalArc = -MinAngle + MaxAngle

local MaxSpeed = 200

while true do
	local Speed = math.floor((Car.Body.VehicleSeat.Velocity.magnitude * 1) + 0) / 1
	
	local SpeedRatio = Speed / MaxSpeed
	
	wait()
end

You’re almost there. SpeedRatio is a percent from 0 to 1. Multiply that by TotalArc to get the percent of the total arc. Then add that to the MinAngle.

For example, a speed of 150 gives a SpeedRatio of 150/200 = 0.75. Then 0.75 * TotalArc = 150 degrees. We want that to be 150 degrees above the MinAngle, so add -100 to it. That becomes 50 degrees.

In code:

local SpeedRatio = Speed / Max speed
local Angle = MinSpeed + SpeedRatio * TotalArc

This is called linear interpolation.

3 Likes