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;
}
}
}