Unity with Vive VR Trigger Animation Collisions

smallarrows.GIF

Written by Jonathan Moore

This document will explain how to use the unity package template for playing an animation on a collision trigger in Unity using the HTC Vive Controllers.

Demo:
You can download a sample Unity demo project and scene file with everything setup here.   (click to download)
    
Save to your downloads folder
     On a PC Right click to Extract All...
     Place the VR Interaction Interaction Demo Scene folder on your desktop
     Open the Assets folder
     Double click VR Demo Scene.unity

or

Use the Unity Package:
Download the animationOnCollision.unitypackage. (click to download)
    
Save to your downloads folder
     On a PC Right click to Extract All...
     Place the animationOnCollision.unitypackage on your desktop
     vr0


Open and Sign into Unity.
Open an existing project or create a NEW 3D project.
Select Assets-> Import Package -> Custom Package
Navigate and find the animationOnCollision.unitypackage file on your desktop.
Click Open
Click All and then Import 


After unity finishes importing you will find the following items:
vr4

The important parts of our package are the animObject, collisionPlay c# script, and the animObject.

These three items can be categorized as:

animObject:  Our Imported Mesh with Animation (exported from Maya or other 3D Software)

animObject Animator.controller:   This is our animation controller.  It is a node type graph that tells Unity how and when to trigger animations for our animObject. 

collisionPlay.cs:
  This is our C# script that triggers the animation when we collide through our mesh.

If we take a look at our animObject you will notice a small arrow next to its icon. 
vr5

If we click here we can see all of the nested / hidden files within our animObject.  Your animObject may already look like this after importing.  If not, click the arrow.
vr6

Take001 is our animation file.  This is the actual exported keyframe animation included with your .fbx model.   Depending on your model or animation you may have several different animation files included within your imported model asset.

In our case, this Take001 is the animation we will be triggering through our Animation controller file (animObject Animation.controller)

If you are using a single animation export from model, you can skip to step 11
.
If you are custom animation with a different name than Take001, follow the instructions below.

To setup a custom animation file / name for your collisionPlay script, we need to change the file name in the Animation controller (animObject Animation.controller).

First, open your Animator window:    Window -> Animator
vr7


Now you should see your Animator Window.  You may have to shift this around to a better view:
vr9


To use this properly we need to select the little eye icon directly under the "Animator" tab.  This will open our layes and parameters.
vr10
The right hand side of the Animator menu is where our Animation "States" are kept.  These states allow us to set up rules for how our animation will be controlled.   In our case, everything is setup, and we only need to change what animation is triggered.

To do this we need to move around our graph to find the Animation node (Take 001).  You can do this by middle mouse click+drag down to find the actual Animation file listed (Take001):
vr11

Click on Take001 and in your inspector window you will see the options to change what animation file is being used:
vr12


Select the small circle next to Take001 and select the proper animation you wish to use.

Set up collisions on animObject::
We need to setup our collision "triggers" on our models.  A collision component will set up an invisible boundary around our object that can be used like a  'switch' to trigger our animation.

Drag your animObject (or other mesh) into your scene hierachy window.
Select your animObject and add a rigid body and a box collider:
Select animObject in Inspector -> Add Component -> Rigid Body
Mass: 1
Use Gravity: Off
IsKinematic:  Checked
Select animObject in Inspector -> Add Component -> Box Collider
Is Trigger:  Checked

Add our C# script to trigger animation:
Our next step is to attach a C# script that activates our animation based on our collision boxes.
Open the Scripts folder -> Select collisionPlay
Drag collisionPlay script onto animObject (or the object you want to animate).
vr13


Setup our HTC VIVE controllers as animation Triggers:
We will be using our Vive hand controllers to "touch" our models and cause them to animate.   Alternatively, you can use any object / mesh to trigger our animation.   This could be a camera, another animated model, etc.

Select your Vive controller in Inspector -> Add Component -> Box Collider
Is Trigger:  Checked
Center:
X: 0
Y
: -0.04
Z: .02
Size:
X: 0.14
Y: 0.07
Z
: 0.05
vr14



Next, add a rigid body:   With the controller slected go to the Inspector -> Add Component -> Rigid Body
Use Gravity: Off
Is Kinematic: Checked

Repeat this step for the right controller.

Fix Unity Bug for Vive Controllers
The last and final step is a bug fix for Unity and HTC Vive.  Currently the steam VR plugin has a bug that causes the Vive controllers to sometimes disappear.  We are going to fix this by adding a simple script component.

Expand the following items in your Heiarchy window:
[Camera Rig] -> Camera (head) -> Camera (eye)

Select Camera (eye) and in the Inspector window select Add Component. 
Search for "Steam VR_Update Poses"
  and add it.
vr15



FIN:    Hit play and test out your triggerable animation!