Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RicardoAntezana · Apr 13, 2017 at 05:50 AM · c#variablevaluevalues

Decreasing a value as another value increases through script

Hello,

I would like to know how to program a line of code that when my first value (x) decreases, my second value (y) increases in a constant ratio. It should be in the ratios like so:

X : Y
100 : 0.15

50 : 0.3

0 : 0.45

Any help is appreciated.

Thank you.

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

4 Replies

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

Answer by Pengocat · Apr 13, 2017 at 09:00 AM

One way could be using InverseLerp.

     private float x;
     private float y;
 
     public float X
     {
         get { return x; }
 
         set
         {
             float tx = Mathf.InverseLerp(0f, 100f, value);
             y = Mathf.Lerp(0.45f, 0.15f, tx);
             x = value;
         }
     }
 
     public float Y
     {
         get { return y; }
 
         set
         {
             float ty = Mathf.InverseLerp(0.45f, 0.15f, value);
             x = Mathf.Lerp(0f, 100f, ty);
             y = value;
         }
     }
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 RicardoAntezana · Apr 13, 2017 at 10:42 AM 0
Share

I haven't seen anything like this, I put this script directly into my code and it doesn't seem to be working. The code is for a vignetting effect going up as my current health gets lower. Here is what I have:

 using UnityEngine;
 using UnityStandardAssets.ImageEffects;
 
      private VignetteAndChromaticAberration vig;
      private float x;
      private float y;
  
      public float X
      {
          get { return x; }
  
          set
          {
              float tx = $$anonymous$$athf.InverseLerp(0f, 100f, value);
              y = $$anonymous$$athf.Lerp(0.45f, 0.15f, tx);
              x = value;
          }
      }
  
      public float Y
      {
          get { return y; }
  
          set
          {
              float ty = $$anonymous$$athf.InverseLerp(0.45f, 0.15f, value);
              x = $$anonymous$$athf.Lerp(0f, 100f, ty);
              y = value;
          }
      }
 
     void Update ()
     {
         vig.intensity = y;
      }


The Health elements are handled elsewhere in the script. All that happens is my vignetting effect is stuck at zero, even though my health goes down.

Also, can you please explain what $$anonymous$$athf.Inversion is, I don't seem to understand the concept and the documentation is not much help.

Thank you.

avatar image Pengocat RicardoAntezana · Apr 13, 2017 at 02:56 PM 0
Share

$$anonymous$$y example is showing how accessors are used when reading and writing to a private variable. This is also known as a C# Property Normally when a Property is used you should no longer use the private field directly. The private field is now accessed through the Property.

 vig.intensity = Y;

InverseLerp has a range between a and b. The 3rd parameter is any value you want to compare to the range a-b.

  1. If the value is = a then 0f will be returned

  2. If the value is = b then 1f will be returned

  3. if the value is halfway between a and b then 0.5f will be returned

The return value is clamped between 0f and 1f so if the value is out of range then it will return either 0f or 1f respectively.

avatar image RicardoAntezana Pengocat · Apr 14, 2017 at 11:30 AM 0
Share

Thank you, I finally understand the concept and got it working!

avatar image
3

Answer by Bilelmnasser · Apr 13, 2017 at 09:39 AM

the easy way to do this is to use Properties when setting a value of first change the value of the other

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
avatar image
3

Answer by chazzysb · Apr 13, 2017 at 05:04 PM

if x is always going to be between 100 and 0 you could implement Y as a simple property

public float Y { get { return ((100 - X) * 0.003f + 0.15f); } }

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
avatar image
2

Answer by UnityCoach · Apr 13, 2017 at 09:51 AM

You could use an AnimationCurve. It's by far the easiest way to handle arbitrary value mapping.

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

339 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 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 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 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# How To Save PlayerPrefs for Unity GUI 2 Answers

Have a problems with a values 1 Answer

C# Custom Class - Declare Variable's Value 1 Answer

Variable value not changing (bug or i'm just an idiot?) 0 Answers

Get Value/String From GUIText 3 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