- Home /
how to change a float in proportion to another float ?
theres a car.velocity.magnitude which varies from 0-12 (can vary from anything to anything, but I want it to vary from 0-12) theres a car sound pitch that varies from 2-3.5 (can vary from anything to anything, but I want it to vary from 2-3.5)
so how can I just define that pitch should remain between 2 and 3.5 and car.velocity.magnitude remain between 0 and 12, and also when car.velocity.magnitude is 0, car sound pitch is 2, when car.velocity.magnitude is half way there, 6, car sound pitch is also half way there, 2.75, when car.velocity.magnitude reaches maximum value, 12, car sound pitch also reaches maximum, 3.5.
something like this.
hope im clear enough, thanks in advanced.
Answer by Pangamini · May 28, 2019 at 11:56 AM
With simple math! FIrst you can do Mathf.InverseLerp() for your car speed, which will map the values 0-12 to 0-1. Actually, computationally cheaper would be simply to divide the speed by 12, right? So when the max. speed is 12, you divide speed by maxSpeed and get a relative speed that ranges from 0 to 1 Next, you want to map this value (linearly I assume) to the range 2 - 3.5, which you can do using Mathf.Lerp(2f, 3.5f, relativeSpeed)
If you want non-linear or customizable map of values between the (relative) speed and pitch, you can also use AnimationCurve, a variable which allows you to draw a curve in the inspector. That way you can simply simulate gear switches (by drawing a curve with rising value, then dropping down a little in the middle, for example)
Your answer
Follow this Question
Related Questions
Change value according to Y position of an object? 2 Answers
(C#) Return Float Variable to previous value 1 Answer
Timer variable set to zero 2 Answers
changing a float number by moving arrows (horizontal and vertical input)? 1 Answer
Change shader property and make all materials using shader change also 1 Answer