• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nanavatidhruvik · May 28, 2019 at 09:58 AM · changefloat

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

(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

Positive to negative not working? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges