# Decrease value at half the speed it's increased at? (using Vector3.Distance)

I have a script that calculates the distance between the player and an obstacle (boat).
In another script, I’m mapping that value between 0 and 1 to adjust the Fill Amount of a UI Image.
What I need to incorporate is when the player is moving further from the boat, the fill amount decreases at half the rate it’s increased at when the player is moving closer to it.
So the player moves close to the boat, it increases quickly, and when they move away, it decreases slowly. Here’s my code for the UI:

Image CircleBarImg;

``````private float stressAmt;

public static float fillAmt;

public float boatReach;

void Start () {
CircleBarImg = this.GetComponent<Image>();
}

void Update () {
stressAmt = boatDistance.dist - boatReach;
fillAmt = MapAnxiety (stressAmt, 0, 100, 1, 0);
if (fillAmt >= 0){
fillAmt = fillAmt * fillAmt; //fillAmt increases exponentially
}
CircleBarImg.fillAmount = fillAmt;
}

private float MapAnxiety(float value, float inMin, float inMax, float outMin, float outMax){
return (stressAmt - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
``````

Thank you

Good day. Do you see you did not make any question?

I will supose you want some way to k ow if aproaching or getting away.

In this cases I always do the same.

Using vector3.distance, i store the distance in a float variable. The nest frame, or using a corrutine i compare that stored distance with the new actual distance, so i can know if during last frame it became closer or not. With this, i change a bool variable called “appreaching”. So, now i can know if its aproacing or not.

The proble. With your problem, is… What to do if i get close to the boat, and at the half of the distance, i stop and commenge going away… What is suposed the alpha to do? I mean at one specific distance, alpha can have 2 different values depending if approaching or not? If you change direction, this value must change instantly? Its strange…

May e the best solutiin is to use always a Lerp function.

Or maybe you just need to forget about this and make alpha always the same for a givven distance.

Byeee!