Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 SirBedlam · Nov 27, 2013 at 05:59 PM · mathfrangecalculatelimits

Problems With Mathf.InverseLerp

I've run into an issue that's driving me insane, because I've read all of the questions on the matter, and although the answers seem to work for the other askers, they simply don't for me. I have an int variable called "boostValue" that I am limiting to a minimum of 0 and a maximum of 300. I have another int variable called "barWidth", which I need to limit to a minimum of 4 and a maximum of about 240, though I'm not set on the exact max value just yet. Now, I need the barWidth value to be set within its range to the equivalent of the boostValue within ITS range. I'll also need to do this for a different int variable similar to barWidth, only I need it to be calculated to a negative value. Either way, I need both of them to be calculated (within their range) according to the current value of boostMeter (within its range). I've tried everything I can find, and everything I can think of, but the barWidth value either goes up by a TINY amount, or just stays at 0. By "tiny amount", I mean I would have to set the boostValue to some ridiculously high number just to get the barWidth to something like 10. I've been messing around with this and trying different ideas for so long now, that I can hardly even remember everything the script has done that isn't what I need. I can't even tell if I'm on the right track anymore. I could be doing something completely different now and I don't even realize it. This is so simple, but it just won't work for me.

 barWidth = Mathf.InverseLerp(4, 240, boostValue);

That's the basis of what I need, though I'm not even sure how I would make it take into account the limits of the boostValue. If someone could please show me what I'm doing wrong here, and maybe show me how I could pull this off, I would be very grateful.

Comment
Add comment · Show 1
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
avatar image robertbu · Nov 27, 2013 at 06:09 PM 0
Share

You lose me with the negative value comment. Your small movement problem may be the result of integer math or the result of a calculation issue. You don't show enough code for me to see what is really going on. But you might be better off using floats and then converting back to an int in the end. Here is your calculation for barWidth:

 barWidth = $$anonymous$$athf.Round(4.0 + (240.0 - 4.0) * (float)boostValue / 300.0);

1 Reply

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

Answer by Roland1234 · Nov 27, 2013 at 06:26 PM

It sounds like you need something like this:

 public static class MathHelper
 {
     public static float RangeValueTranslate(float start0, float end0, float value0, float start1, float end1)
     {
         var t1 = (value0 - start0) / (end0 - start0);
         return start1 + ((end1 - start1) * t1);
     }
 }

which you would use like this:

 var barWidthValue = MathHelper.RangeValueTranslate(boostValueMin, boostValueMax, boostValue, barWidthMin, barWidthMax);

Having to cast if necessary, but it shouldn't be a problem (just note that you will lose precision if casting your result to an int). Should work fine for ranges going in either direction (negative or positive).

Oh - and watch out for divide by zero possibilities.

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

How to use Mathf.Lerp and Mathf.InverseLerp??? 1 Answer

Map the difference of two numbers to a certain min and max range 2 Answers

How to calculate the width of the Rect, knowing current value and min and max values 0 Answers

get the height of a sprite at a specific point 0 Answers

Range repeat? like 50 to 100 then back to 50 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges