VR Buttons In Unity
Here we’ll cover two quick ways we can get pressable vr buttons working inside of Unity using OpenVR. The first leverages SteamVR’s Unity integration and the second a custom script without any dependencies.
Here we’ll cover two quick ways we can get pressable vr buttons working inside of Unity using OpenVR. The first leverages SteamVR’s Unity integration and the second a custom script without any dependencies.
Here we’ll cover two quick ways we can get pressable vr buttons working inside of Unity using OpenVR. The first leverages SteamVR’s Unity integration and the second a custom script without any dependencies. This post assumes you are running Unity with SteamVR/OpenVR and are comfortable setting up a basic VR environment. Let’s get started!
If you are coming from the video, you can jump straight to the scripts section.
If you are a visual learner, here is a setup video that will walk you through how to setup the scene and the two VR button components that are covered below. It doesn’t cover creating the Button.cs and Events.cs scripts but shows you how to use them.
If you are already using the SteamVR Asset Store package, then you can get pressable VR buttons very quickly using the packages provided scripts.
Now you should be able to play your scene and look around in VR! You may need to adjust your table cube’s z position slightly if it’s too close.
Your button should now respond to your hands and move down when pressure is applied, calling your ButtonHandler’s method when it reaches the distance you put in at step 4.
In this section, we’ll cover a very fast way to get the pushing feel and functionality on a VR button with very little setup and code needed. You’ll need the Button script below as well as Events script to handle what happens when your button is pressed. You can find these in the scripts section.
Follow the SteamVR’s Hover Button Environment section above. If you aren’t using the SteamVR prefab, that’s ok! Just make sure that your controllers/hands have a collider attached that isn’t a trigger and can interact with our button.
You should now be able to push the button down with your hand, or collider on your controller and see that it runs the Events.cs method when it gets to the bottom. Cheers!
Because this approach uses a spring and active colliders, you can’t have it go through other game objects with colliders on them. You’ll have to either turn off the Table’s box collider or set the button on a different layer and under Edit -> Project Settings -> Physics -> Layer Collision Matrix, disable their ability to collide.
using UnityEngine;
using UnityEngine.Events;
public class Button : MonoBehaviour
{
[System.Serializable]
public class ButtonEvent : UnityEvent { }
public float pressLength;
public bool pressed;
public ButtonEvent downEvent;
Vector3 startPos;
Rigidbody rb;
void Start()
{
startPos = transform.position;
rb = GetComponent<Rigidbody>();
}
void Update()
{
// If our distance is greater than what we specified as a press
// set it to our max distance and register a press if we haven't already
float distance = Mathf.Abs(transform.position.y - startPos.y);
if (distance >= pressLength)
{
// Prevent the button from going past the pressLength
transform.position = new Vector3(transform.position.x, startPos.y - pressLength, transform.position.z);
if (!pressed)
{
pressed = true;
// If we have an event, invoke it
downEvent?.Invoke();
}
} else
{
// If we aren't all the way down, reset our press
pressed = false;
}
// Prevent button from springing back up past its original position
if (transform.position.y > startPos.y)
{
transform.position = new Vector3(transform.position.x, startPos.y, transform.position.z);
}
}
}
using UnityEngine;
using Valve.VR.InteractionSystem;
public class Events : MonoBehaviour
{
public void OnPress(Hand hand)
{
Debug.Log("SteamVR Button pressed!");
}
public void OnCustomButtonPress()
{
Debug.Log("We pushed our custom button!");
}
}
At SLIDEFACTORY, we’re dedicated to turning ideas into impactful realities. With our team’s expertise, we can guide you through every step of the process, ensuring your project exceeds expectations. Reach out to us today and let’s explore how we can bring your vision to life!
Looking for a development partner to help you make something incredible?