Hi all.
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 triggers haptic feedback when the controllers are held in the vicinity of a Sound Sphere. I am trying to accomplish this using a Vector3.Distance function that compares the location of each controller and Sound Sphere, every frame. When the distance is less than 0.4 units, haptics should be fired in the specific controller that is near a Sound Sphere at that moment in time.
I am running into issues when I attempt to compare the locations of every GameObject (controllers and Sound Spheres) every frame. How could I create a variable that contains the Vector3.transform.position of every controller, every frame? Concurrently, how could I create a variable that contains the Vector3.transform.position of every Sound Sphere, every frame?
My pseudo-code solution is here. This code at the moment does not run because I receive the error message “Cannot implicitly convert type ‘UnityEngine.Vector3’ to ‘UnityEngine.Vector3’”. Am I going in the right direction by trying to create an array of Vector3 values and them comparing them every frame? Or should I be trying a different approach? Your help would be greatly appreciated.
public class ControllerHaptics : MonoBehaviour {
SteamVR_TrackedObject trackedObject;
SteamVR_Controller.Device device;
[SerializeField] GameObject[] controllers;
[SerializeField] GameObject[] soundSpheres;
Vector3[] controllerPositions;
Vector3[] soundSpherePositions;
float distance;
// Use this for initialization
void Start()
{
trackedObject = GetComponent<SteamVR_TrackedObject>();
device = SteamVR_Controller.Input((int)trackedObject.index);
controllers = GameObject.FindGameObjectsWithTag("Controller"); //VR Controllers
soundSpheres = GameObject.FindGameObjectsWithTag("Grabbable"); //Sound Spheres
}
//Update is called once per frame
void Update()
{
GetControllerPositions();
GetSoundSpherePositions();
FireHapticsBasedOnDistance();
}
void GetControllerPositions()
{
for (int i = 0; i < controllers.Length; i++)
{
controllerPositions = controllers*.transform.position;*
}
}
void GetSoundSpherePositions()
{
for (int i = 0; i <soundSpheres.Length; i++)
{
soundSpherePositions = soundSpheres*.transform.position;*
}
}
void FireHapticsBasedOnDistance()
{
distance = Vector3.Distance(controllerPositions, soundSpherePositions);
if (distance < 0.4)
{
device.TriggerHapticPulse(500);
}
}
}