• 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 zardini123 · Jul 22, 2015 at 03:55 AM · algorithm

Algorithm Help

Hello. I am having troubles creating a algorithm that fits my needs.

So I have a minimum value, a maximum value, and a control value.

The control value can go below the minimum value and above the maximum value.

I need it so the output value is 0.0 when Control <= Minimum, and 1.0f when Control >= Maximum.

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

3 Replies

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

Answer by zardini123 · Jul 22, 2015 at 03:54 PM

Okay I figured it out. The Mathf.Clamp helps a little.

 (Mathf.Clamp(control, min, max) - min) / (max - min);
Comment
Add comment · Show 3 · 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
avatar image meat5000 ♦ · Jul 22, 2015 at 04:32 PM 0
Share

I was thinking along the lines of an Inverse Lerp. Where you would put in your float and it would throw out t between 0 and 1.

avatar image zardini123 · Jul 22, 2015 at 04:38 PM 0
Share

So to help you understand my algorithm:

Say: $$anonymous$$ = 1 max = 3 control = 2

So the $$anonymous$$athf.Clamp() keeps control between $$anonymous$$ and max, but right now that doesn't matter, so now we have: = (2 - $$anonymous$$) / (max - $$anonymous$$)

So now if we plug in the $$anonymous$$ and max: = (2 - 1) / (3 - 1) = (1) / (2) = 0.5

avatar image meat5000 ♦ · Jul 23, 2015 at 11:50 AM 0
Share

So there is a $$anonymous$$athf.InverseLerp already! :)

avatar image
1

Answer by kidmosey · Jul 22, 2015 at 05:42 AM

Mathf.Clamp(control, min, max)

Alternatively, if you need to include min/max, you have (similar to the suggested if/else block):

 Mathf.Max(min, Mathf.Min(max, control)) 

Comment
Add comment · Show 3 · 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
avatar image Vipul_Gandhi · Jul 22, 2015 at 05:50 AM 0
Share

Hi,

If I have correctly understood your problem,

 if(control > max)
 {
 //Debug.log("1")
 }
 else if(control <= $$anonymous$$)
 {
 //debug.log("0");
 }

Thanks

avatar image zardini123 · Jul 22, 2015 at 03:49 PM 0
Share

$$anonymous$$athf.Clamp doesn't get exactly I need. $$anonymous$$athf.Clamp clamps the number to be $$anonymous$$ and max. It doesn't allow the output to be 0 at $$anonymous$$ and 1 at max.

avatar image kidmosey · Jul 23, 2015 at 04:11 PM 0
Share

I guess you could also try $$anonymous$$athf.$$anonymous$$ax($$anonymous$$, $$anonymous$$athf.$$anonymous$$in(max, control))

avatar image
-2

Answer by realbambiiyt · Jan 06, 2021 at 11:53 PM

Hello! I know this is a very old thread, but I have come across it because I have a similar issue to the one OP has. For context, I am using the distance between two position vectors for the control variable, a min of 1, and a max of 20. The goal is that the closer I get to my target position, the output value of the algorithm increases from 0 to 1. However, with the solution OP has suggested I do the exact opposite, decreasing from 1 to 0 the closer I get. Any help would be really appreciated, but I'll continue to toy with this for a little while too.

Comment
Add comment · Show 1 · 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
avatar image Bunny83 · Jan 07, 2021 at 02:16 AM 0
Share

This is not an answer to the question. Please ask a seperate question. This is a Q&A site and not a forum.

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

26 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

Related Questions

Get the point between 2 other points using weights 1 Answer

Unity 3.5: What is the definition of PLE algorithm? 1 Answer

Check for closed circuit of pickups 2 Answers

Beat detection algorithm 4 Answers

Two camera rendering an object differents textures 2 Answers


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