Skip to content

Instantly share code, notes, and snippets.

@Sarthakg91
Last active August 2, 2019 22:13
Show Gist options
  • Save Sarthakg91/da7798e24849fa9c4a6f1fb49e06e349 to your computer and use it in GitHub Desktop.
Save Sarthakg91/da7798e24849fa9c4a6f1fb49e06e349 to your computer and use it in GitHub Desktop.
button press detection
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class BaseColliderControl : MonoBehaviour
{
public GameObject rightController;
public GameObject leftController;
public SteamVR_Action_Vibration hapticAction;
public SteamVR_Input_Sources hand;
private bool waitOn;
// Start is called before the first frame update
void Start()
{
waitOn = false;
}
// Update is called once per frame
void Update()
{
}
IEnumerator waitForSecs(float f)
{
waitOn = true;
yield return new WaitForSeconds(f);
waitOn = false;
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Enterred collison" + other.name);
string object_name = other.name;
if (!waitOn && object_name.Contains("StartButton"))
{
if (rightController.GetComponent<ControllerCollisonDetector>().IsColliding())
hapticAction.Execute(0.0f, 0.1f, 50.0f, 0.2f, SteamVR_Input_Sources.RightHand);
else if (leftController.GetComponent<ControllerCollisonDetector>().IsColliding())
hapticAction.Execute(0.0f, 0.1f, 50.0f, 0.2f, SteamVR_Input_Sources.LeftHand);
GameObject button_text = other.transform.Find("ButtonText").gameObject;
button_text.GetComponent<TextMesh>().color = Color.cyan;
waitForSecs(0.15f);
}
}
void OnTriggerExit(Collider other)
{
string object_name = other.name;
if (!waitOn && object_name.Contains("StartButton"))
{
if (rightController.GetComponent<ControllerCollisonDetector>().IsColliding())
hapticAction.Execute(0.0f, 0.1f, 50.0f, 0.2f, SteamVR_Input_Sources.RightHand);
else if (leftController.GetComponent<ControllerCollisonDetector>().IsColliding())
hapticAction.Execute(0.0f, 0.1f, 50.0f, 0.2f, SteamVR_Input_Sources.LeftHand);
GameObject button_text = other.transform.Find("ButtonText").gameObject;
button_text.GetComponent<TextMesh>().color = Color.white;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment