Slowly increase a var when two objects get closer together

I need a variable to increase when two objects get closer together.

health -= health * decreaseRate;

I need the decreaseRate to increase the closer the player is to an object. How would I go about doing this, mathematically?

1 Like

(some constant) / (distance) is one option.

e.g.

5 / 1 = 5
5 / 5 = 1
5 / 10 = 1/2

1 Like

But then the number gets smaller, not bigger.

5 / (distance)

5 / 1 = 5
5 / 5 = 1
5 / 10 = 0.5

I need the number to get bigger, not smaller

1 Like

Take a look at the numbers again - the result is bigger the closer you are.

1 Like

Oh right, I feel stupid now :slight_smile: Thanks heaps

1 Like

im sorry but i dont understand a thing can you use it in a script please

1 Like
            float currentDistance = Vector3.Distance(transform.position, targetObj.position);

            GetComponent<AudioSource>().PlayOneShot(audioClip);
            GetComponent<AudioSource>().pitch = 10/currentDistance;

I hope this helps :wink: