How can I increase a value while another value decreases: inverse correlation during Update?

I am building an interactive VR experience using the HTC Vive and the SteamVR Unity plugin. My experience involves navigating a space filled with “Sound Spheres” that generate sound when colliding with the Vive controllers. These Sound Spheres are static i.e they sit still in space. I am attempting to write code that helps users locate the spheres using haptic feedback. Currently, I have simple code that measures the distance between a controller and each Sound Sphere. Once the controller is close to the Sound Sphere - haptics fire at a constant intensity.


However, I would like the haptics to increase in intensity as the controller approaches the Sound Sphere. In other words, the intensity should increase as the distance between the controller and the Sound Sphere decreases. I am unsure how to increase one value while the other decreases on update. Please advise. Functional code is below:

public class ControllerHaptics : MonoBehaviour {

    SteamVR_TrackedObject trackedObject;
    SteamVR_Controller.Device device;

    GameObject[] soundSpheres;

    // Use this for initialization
    void Start()
    {
        trackedObject = GetComponent<SteamVR_TrackedObject>();
        device = SteamVR_Controller.Input((int)trackedObject.index);

        soundSpheres = GameObject.FindGameObjectsWithTag("Grabbable");
    }

    //Update is called once per frame
    void Update()
    {
        FireHapticsBasedOnDistance();
    }

    private void FireHapticsBasedOnDistance()
    {
        foreach (GameObject soundSp in soundSpheres)
        {
            // controller approaches Sound Sphere
            if ((transform.position - soundSp.transform.position).magnitude < 0.5f) //if the distance between the controller and the Sound Sphere is less than 0.5 then:
            {
                //we have found a Sound Sphere close to a controller
                device.TriggerHapticPulse(600); // <------- this value should increase as the above magnitude decreases. note that this value is a "ushort" value and probably needs to be converted? 

                break;
            }
        }
    }

I would have a float thats called originalValue for example and set that to the value you want to change in start. Then you set your value that you want to change in update to originalValue / distance.

Edit: I don’t know if I understood the question correctly or not but I hope this helps :smiley:

You just need to create a ratio that maps the min/max distance range to the min/max haptic pulse range. Something like below. (You’ll need to adjust units and range values.)

    float minDist = 0;  // meters
    float maxDist = 1;  // meters
    float minDistHaptic = 1000;  // Haptic units
    float maxDistHaptic = 0; // Haptic units
    
    float dist = <distance between objects>
    float hapticValue = minDistHaptic + ( dist - minDist ) / ( maxDist - minDist ) * (maxDistHaptic - minDistHaptic);
    hapticValue = Mathf.Clamp( hapticValue, maxDistHaptic, minDistHaptic );