Unity with Vive VR Trigger Animation Collisions

smallarrows.GIF

Written by Dino Zhang and Jonathan Moore

This document will explain how to play an animation on a collision trigger in Unity using the HTC Vive Controllers.

Play animation on collision tutorial
Create a new Unity Scene, Select NEW
Set to 3D -> Create Project


Connect the VR headgear and sensors for the Vive


In Unity:
Start Unity
(You may need to set up an account)
Select NEW

Give your project a name such as Maya_Tutorial. This will create a folder with folders inside.
Save to the Desktop.
Select 3D.
Select Create Project.

(Close the Services tab, right mouse over the tab, select Close Tab if Services is there.)


Import a VR Camera and Sensors for Vive
     Choose Window->Asset Store

     Type Steam VR Plugin in the search bar, also select FREE ONLY button
unity_VR_steam_plugin
Select the
Steam VR Plugin (the free one by Valve Corporation) and Download

Choose Import
Choose Accept All and OK a few times

Put the main view window back to Scene instead of Asset Store

     Click the Scene tab on the left

Open the arrow on the SteamVR folder from the Project tab
Select Prefabs
Drag the CameraRig into the Hierarchy tab
In the Inspector tab under Steam VR_Play Area
change Size to Calibrated, this uses the calibrated area from the headset


Add RigidBodies and colliders to Vive hand held controllers
Open the arrow for CameraRig
in the Hierarchy tab choose both of the Controllers

In the Inspector tab
: select Add Component -> Physics->RigidBody
     Turn off  Use Gravity

In the Inspector tab: select Add Component -> Physics-> Box Collider
     Input Center:    X: 0   Y: -0.04   Z: .02
     Input Size:   X: 0.14   Y: 0.07   Z: 0.05



Main Camera Settings
Select the Main Camera from the Hierarchy tab
In the Inspector tab
     Change Target Display
to Display 2.

     Change the Target Eye to None



Import Maya Models:
In Maya:
Make an object, animate the y rotation. Make the timeline the same length as the animation.
Choose File->Save Scene As  select Your Unity project folder location -> Assets ->Save as
If you have objects that don't move place them in a separate Maya file. If you have other objects that animate, give them each their own separate files.




In Unity:

Your model should now show up in the Assets folder under the Project tab. Click on Assets to see the contents.

Select the Maya object you animated in the Assets tab, in the Inspector tab, go to the Model tab and change Scale Factor to 0.3
Hit Apply


VRCollisions_1

Drag and drop the animated object into the Hierarchy tab.


Create an Animator Controller:
*The Animator Controller allows you to arrange and maintain a set of animations for an object, as well as transitions between the different animation states.

Top menu bar  Assets-> Create-> Animator Controller re-name it

Select the object that has the animation from the Hierarchy window

In the Inspector, Animator should be checked

Click the small round circle to the right of Controller, select the controller you just added.
unity_link_anim_controller




Set up Animation States:

Double click on AnimController in Assets, the Animator tab should pop up.
     (If it didn't pop up, go to top menu bar -> Windows -> Animator.)
To navigate inside the Animator Tab, Hold middle mouse button and drag on the grid.


VRCollisions_4

Right click
on the grid select Create State -> Empty.
Select New State  Rename this state in the Inspector to Idle -> Hit Enter on the keyboard to save the name.

VRCollisions_5

*Notice in the animator tab, this Idle state is orange, which means it is the default state.

VRCollisions_6


Next, we want to insert the rotation animation that we created in Maya into the animator tab.


Open up the little arrow next to the animated object in the Assets folder
Drag the Animation Take 001 into the Animator tab onto the grid.

VRCollisions_8



Set up Transitions:
*In order to have the animations go from "not moving" to "moving", we need to add some transitions.

Right click on the
Idle state, select Make Transition, Click on the Take 001 state to connect them.
Now you will see a line with an arrow pointing at the Take 001 state.

VRCollisions_11

You might want your object to return to its original non moving state after playing the animation once:
Right click on the Take 001, select  Make Transition, Click on Idle.


Add a Parameter:
*To trigger the transition in the c# script, we need to add a Parameter as a condition.

The Parameters tab is on the left side of the Animator.
     (If this doesn't show up, click on the little eye next to Base Layer.)
Click on the + below the little eye to add a parameter  select Trigger.
Double Click on the New Trigger to change its name to playAnim.

VRCollisions_12

Click on the 1st transition (line with the arrow inside the animator tab).
Inside Inspector: Click on + under Conditions, playAnim should pop up. If not, type it in.

VRCollisions_13

Now your animations are all set up.



Setting up collision on the animated object:
Select the animated object in the Hierarchy tab

In the Inspector tab
Select Add Component, then select Physics->Rigidbody
     Mass: 1
     Use Gravity: Off
     Select Is Kinematic: Check  (This will let it play the animation with no physics.)

Select
the animated object in the Hierarchy tab again
In the Inspector tab 
Select Add Component  then select Physics-> Box Collider



Add a Script to start the animation from a collision:
Select the animated object in Hierarchy.

In Inspector: 
Add Component
, then select
New Script   (scroll down)
Type in collisionPlay
Select Create and Add
Double click on collisionPlay in the Assets folder, the MonoDevelop script editing software should pop up. If MonoDevelop does not show up set the Preferences.

Delete
everything, copy and paste the script below:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collisionPlay : MonoBehaviour {

 
Animator anim;

 
// Use this for initialization
 void Start () {
  anim = GetComponent <
Animator > ();
 }

 
void OnCollisionEnter(Collision collision) {
  anim.SetTrigger (
"playAnim");  // collision starts animation
 }  
 
void OnMouseEnter() {  // for testing, mouse enter starts animation too
  anim.SetTrigger ("playAnim");
 }
}



Save the script, Select  File -> Save.   File->Quit
Note inside the anim.setTrigger is our parameter "playAnim", when the Vive controllers touch the animated object, it will play the animation.
In addition, you can touch the animated object with your mouse to start the animation as well.

Press Play in Unity to test it out.



Collision Objects such as the ground
Add Rigidbody,   turn off    Use Gravity
Add Colliders, Check Is Kinematic

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 AnimationOnCollision folder on your desktop
     Open the Assets folder
     Double click AnimOnCollision.unity



Screen Grab software for documenting VR projects:
OBS Studio https://obsproject.com/download 
or
Game Bar should come with Windows 10
Screen Capture YourVR  Project Windows 10, Game Bar, Xbox App
Windows 10, Win Key + G to open, WIN Key + Alt to record