5 Minute Script: Fire Away with the HTC Vive Controller

Blog Posts ,Programming ,Tutorials ,Unity ,Virtual Reality ,Vive
July 4, 2016

Hey everyone!

If you missed it, my last episode of Just A/VR Show covers some of the basics of getting setup with the SteamVR plugin in Unity for the HTC Vive – I’ve been working on a sample project using these and today’s 5 Minute Script covers the basics of setting up the controllers to fire at something! The full tutorial for this will be out shortly, but I wanted to share the basics of working with a Vive [SteamVR_Tracked] controller, since it’s handled a little differently than controllers usually are in Unity!

When you use the SteamVR plugin, you’ll get all of the tracking with the Lighthouse, Chaperone, and the controllers for free – Valve has done an awesome job with their work on making it easy to develop for the Vive – and customize the behaviors as makes sense for your game or application.

I made a really simple shooting effect to pop bubbles in my application, which only involved a few steps:

  • Set up the SteamVR Camera Rig
  • Attach the script below to the controller

Since I’m right-handed, I chose to attach the script to the right hand controller for better aim. The script is really simple – in my application, it communicates with a game controller to pop and generate a bubble, but you can write your own custom behaviors in the script that you assign to the ClickedEventHandler on your controller.

First, I set up my global variables at the top of the script, which I called “PlayerScript”:

 BubbleScript _activebubble;
 SteamVR_TrackedController controller;

The BubbleScript variable is specific to my application – I’ve included it for continuity, but you most likely wouldn’t include that information in your own app. You will want a variable to store the Vive controller, so at a minimum, make sure to include the tracked controller. Once you have those, you’ll need to initialize the SteamVR controller in the Start() method like so:

void Start () {
    controller = GetComponent<SteamVR_TrackedController>();
    if (controller == null)
        controller = gameObject.AddComponent<SteamVR_TrackedController>();
    controller.TriggerClicked += new ClickedEventHandler(Fire);

The first line of the above method attempts to find the SteamVR_TrackedController component on the object, and adds one if it’s null. Then, we add a ClickedEventHandler() and specify the function ‘Fire’ as what should be called when the trigger on the controller is clicked. All we have to do is implement the Fire method, and we’ll be good to go with our first controller behavior!

 // Fire when trigger on controller clicks
 void Fire(object sender, ClickedEventArgs e)
   int layerMask = 1 << 8;
   RaycastHit _hit;
   if (Physics.Raycast(transform.position, transform.forward * 10, out _hit, 10.0f, layerMask))
     _activebubble = _hit.collider.gameObject.GetComponent<BubbleScript>();

The Fire function sets off a raycast from the gameObject it’s attached to (the controller) and tests if there is a collision. If so, we “pop” the bubble by sending a message to the active bubble. Super straightforward – and now, when we click our trigger on the Vive controller, we’ll also be shooting invisible rays into the bubbles!

This, of course, can be helpful for a number of different reasons. You can use this in conjunction with the built-in laser pointer script to interact with different interface elements. You can use it as a “grab” motion to pick up and move things within your scene (check out NewtonVR if you’re looking for a physics package!) and the possibilities are endless. This is a basic implementation, but shows an easy way to add behaviors to your controllers within your application.

[ ]-)


Related Posts

1 thought on “5 Minute Script: Fire Away with the HTC Vive Controller”

  1. Anonymous says:

    Thank you for this tutorial. Loved it.

Leave a Reply