- Home /
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.1550 : 0.3
0 : 0.45
Any help is appreciated.
Thank you.
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;
}
}
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.
$$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.
If the value is = a then 0f will be returned
If the value is = b then 1f will be returned
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.
Thank you, I finally understand the concept and got it working!
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
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); } }
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.
Your answer
Follow this Question
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