Unity, Vive VR, Trigger Animation with Collisions

smallarrows.GIF

Older, now using VRTK     Advanced
This document will explain how to play an animation with a collision trigger in Unity using the HTC Vive Controllers.


In Unity:

Start Unity,
Select NEW, Select 3D.



Import SteamVR SDK Assets
Choose Window->Asset Store

Type SteamVR in the search bar
unity_VR_steam_plugin
Select the
SteamVR Plugin by Valve  (It is free)
Choose Import twice (Ignore errors, select I made a backup go ahead)
Choose
Accept All and OK

Select the SteamVR folder from the Project tab, Select the arrow, select Prefabs
Select the CameraRig
In the Inspector tab
change Size to Calibrated, this uses the calibrated area from the headset

Drag the [CameraRig] into the Hierarchy tab
Unity_steam_primitives1



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.12   Y: 0.07   Z: 0.05



Setup the Main Camera to be used later
Select the Main Camera from the Hierarchy tab
     Change Target Display to Display 2

     Change Target Eye to None in the Inspector tab
(The main camera interferes with the Vive camera if you do not do this.)



In Maya: Model an animated object
Animate a poly object in Maya, keep it short, 30 frames 1 second, frame 1 should be the same as the last frame.
Set the timeline to end at 30 frames.

Save the Maya scene file into your Unity project-> Assets
You will see your models and textures appear automatically in Unity.



In Unity: Add your Maya File to your scene
Select Assets in the Project tab
Drag your Maya file from the Assets list over to the Hierarchy tab.

If your object is the wrong scale do this:
Select the Maya file in the Assets list, select the Model section in the Inspector tab, change the Scale Factor .3, hit Apply

VRCollisions_1



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_dot
unity_link_anim_controller




Set up Animation States:

Double click on AnimController in Assets, the Animator tab should pop up.

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 animation that we created in Maya into the animator tab.


Open up the little arrow next to the animated object in the Assets folder   unity_asset_arrow
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).
     Uncheck   Has Exit Time  in the Inspector tab
Inside Inspector: Click on + under Conditions, playAnim should pop up.
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
     Use Gravity: Off
     Select Is Kinematic: Check  (This will let it play the animation with no physics.)
     Select Collision Detector    Continuous

Select
the animated object in the Hierarchy tab again
In the Inspector tab 
Select Add Component  then select Physics-> Box Collider  (or a shape that matches your object)



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 Edit->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 trigger animation
 }
 
void Update() {
 
if (Input.GetKeyDown("space"))  //spacebar
   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.



Demo File:
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



CHECKLIST
CameraRig  Calibrated

Controllers
RigidBody
     Turn off  Use Gravity
Box Collider

     Input Center:    X: 0   Y: -0.04   Z: .02
     Input Size:   X: 0.12   Y: 0.07   Z: 0.05

Main Camera
     Target Display to Display 2

     Target Eye to None

Animator Controller
     Select the object that has the animation  Controller  unity_dot should be linked in the Inspector
     Double click Animator Controller, Add a state, rename Idle
     Drag Take001 to Controller
     Right mouse make transition from Idle to Take 001
     Right mouse make transition from Take001 to controller

Parameters tab is on the left side of the Animator.
     Click on the + below the little eye to add a parameter  select Trigger.
     Rename playAnim.

Click on the 1st transition (line with the arrow inside the animator tab).
     Uncheck   Has Exit Time  in the Inspector tab
     Inside Inspector: Click on + under Conditions, playAnim should pop up.
Create the c# script