Tuesday, October 21, 2014

RAZER HYDRA IN UNITY3D

Priyankana Basak, Mounika Chillamcherla, Logan Sales

Razer Hydra is a motion and direction controller developed by Sixsense Entertainment.


It consists of 2 controllers with 5 Digital buttons, 1 analog stick/button, 1 bumper button and 1 analog trigger. It is a wired and has a range of 1 mm and 1°.

There are six degrees of freedom associated with the Hydra. i.e :
  1. forward/backward
  2. up/down
  3. left/right
  4. pitch
  5. yaw
  6. roll
The right controller has the four buttons used for :
  1. Pointing
  2. Spreading/ Grip Ball
  3. Pick up/ Hold Book
  4. Folding ( 1st and 5th finger)

The left controller has the four buttons used for :
  1. Spreading/ Grip Ball
  2. Pointing
  3. Folding ( 1st and 5th finger)
  4. Pick up/ Hold Book

INTEGRATING WITH UNITY

Prerequisite: Unity Pro version with the .dll plugin is needed to handle the mobile functions.
To integrate the Razer Hydra with Unity, the following steps should be followed :

Sixense provdes their own asset through the Unity Asset Store: https://www.assetstore.unity3d.com/en/#!/content/7953. Download the plugin and import the assets.
Create a project and create a scene. Import the assets in the scene.


Create an empty GameObject in the Hierarchy window to serve as the player, here we will attach various child assets.

Then copy the SixenseInput object from the SixenseInput asset. This contains a HandController with a right and left hand, as well as scripts for controlling them through Unity.

PROJECT RAZER TEST:

Following the steps above, the hands appear on the scene, as we can see below.


The left hand corresponds to the left controller whereas the right hand corresponds to the right controller.

The tasks for the right controller are :
  • The joystick rotates the scene in X and Z directions
  • Button 1 is used to point and disable an object for 15 seconds
  • The trigger is used to grab and place an object at its original and/or different place


The tasks for the left controller are :
  • The joystick moves the scene in X and Z directions
  • The controller is used to push the objects in space
The player is given a capsule collider and rigid body with a mass of 60 units to make the movements in the scene appropriate.

MAKING THE LEFT CONTROLLER TO MOVE FORWARD AND BACKWARD IN THE SCENE

For this, initialize the following in the Start() function in the CharacterMovementController.cs script:
  • rotationX and rotationY to 0
  • sensitivityX and sensitivityY to 150
  • minimumX to -360 and maximumX to 360
  • minimumY to -60 and maximumY to 60
  • movementSpeed to 10f
In the FixedUpdate() function, insert the following code :


When the joystick of the left controller is moved in X and Y directions, the player moves in the X and Z directions respectively.

Based on the movement speed, sensitivities, and time, a calculated relative force is applied to the rigid body of the player.

MAKING THE RIGHT CONTROLLER TO ROTATE THE SCENE

The same initializations (as for the left controller) should be applied to the right controller in the Start() method of the CharacterMovementController.cs script.

In the FixedUpdate() function, insert the following code :


When we rotate the joystick of the right controller, across X and Y directions, the player rotates in the respective directions.

The rotations take place only if the value of joystickY is greater than 0.05f or less than 0.0f. We did this because we noticed that sometimes the controller had a very small value when it should have been set to 0.0. You can test this with the SixenseInputTest scene.

The angle of rotation is calculated using local euler angles and the rotations are applied in the respective directions.

You may notice that we’re not applying these rotations to the same transformation. We only want the player to turn from side to side while the attached camera rotates up and down. This way, the forward direction for movement can be directly applied to the player object and the camera will turn with it. If we applied the vertical rotation to the player, the capsule collider would cause the camera to lean forward awkwardly.

FUNCTIONALITY OF POINTING AT OBJECTS:

Button 1 of the right controller is used to point at various objects located in the scene.


The following code is written in the Update() function of the MagicPointing.cs script.


Here, we are using RaycastHit to point at the objects and get their name.

If the player is hitting the object with name “cylindrical Glass”, it gets disabled for 15 seconds.


The code for doing this action is present in another script called GlassDisable.cs using a public Disable() function.


FUNCTIONALITY OF PUSHING OBJECTS

The left controller is used to push objects located in the scene.

Original position:



Position after pushing :


This is possible as there is a capsule collider attached to the left hand.

FUNCTIONALITY OF GRABBING OBJECTS 

Trigger button of the right controller is used to grab various objects located in the scene.


The following code for grabbing is as follows:


Here we check where the right hand is within touching distance of an object while the player is holding down the trigger. If the player is trying to grab an object with a rigid body (that also happens to not be the player) we set the position and rotation of the object to match the right hand while disabling a majority of the objects physics.

The FixedUpdate() function then checks whether the player is letting go of an object to restore it’s physics.

FUTURE ENHANCEMENTS

  1. Only grabbing one object at a time. We leave it up to the reader to determine whether this is a feature or a bug. When the hand is touching more than one object, both will be set to position of the hand when grabbing. One way to do this could be to only grab the object that is closest to the hand.
  2. Grabbing through physics. Another way to grab objects could be through the colliders of the hands. A flat surface on the palm side is recommended for easier gripping. Without any tweaking, this sort of functionality can be very difficult to achieve with any success, as it is arguable much more difficult to use a virtual controller in place of ones own hands.

1 comment:

  1. Thank you for this tutorial.

    It is very intuitive and helpful.

    Do you have any instructions on how to integrate hydra with oculus and unity 5?

    thanks

    ReplyDelete