Leap Enabling the Unity3D Car Tutorial

In this Blog, I take a Tutorial from unity 3D (The Car Racing Tutorial) and I modify it to be LeapEnabled and supply below the step by step instructions for the reader to be able to reproduce this.

Files and Resources Needed.

If you are interested in the final result. Check out this Video of what it looks like.

Step 1 Create a new project. 

I called mine LeapEnabledCarTutorial
Make sure you check the flowing packages to import “Car Tutorial.unitypackage”.  If you do not have this package on your machine, go ahead and download it from the Unity Asset store (it’s free) 

step1

Step 2 Make sure that the Complete Scene is working properly

Open the CompleteScene unity scene and run it. Make sure the car comes up and you can go forward with the up arrow and steer with the left and right arrow

 step2

Step 3 Duplicate the Complete Scene so you can experiment

Choose edit.. Duplicate Scene

This will create a duplicate of the CompleteScnee called CompleteScene 1

I renamed mine to LeapCompleteScene

step3

Step 4 Create a Plugins folder under the asset directory

Step 5 Copy the Leap Files from UnityAssets in the SDK to your Plugins Directory and root directory

If you have unity Pro, you can copy the 3 files (Windows) from the Leap UnitySandbox\Assets\Plugins directory to your newly created Plugins director
If you have Unity Free only, then you need to copy Leap.dll and LeapCSharp.dll to the root directory of your game, and LeapCSharp.NET3.5.dll to your Plugins directory.
For more information on getting Unity Standard (free) working with Leap, read my Blog (http://pierresemaan.com/getting-the-leap-to-work-with-unity-free-version-not-pro/ )

step5astep5b

Step 6 copy LeapUnityExtensions.cs to the Plugins folder

LeapUnityExtensions.cs is in the Assets\Scripts directory from the examples in the SDK under UnitySandbox.

Step 7 copy pxsLeapInput.cs to the Plugins folder

Download pxsLeapInput.cs (see the top of the Tutorial for all downloads) and copy the script to the Plugins folder.  The Script has to be in Plugins as we need to reference it from a javascript script later and it needs to be compiled first (so the javascript engine can see it)

Step 7 a (optional) examine the pxsLeapInput Script

Note that it is a Classic class (not Monobehavior).  It’s a singleton and includes properties to easily get Frame and Hand from the latest Update.

The key function is GetHandAxisPrivate

private static float GetHandAxisPrivate(string axisName, bool scaled)
{
// Call Update so you can get the latest frame and hand
Update();
float ret = 0.0F;
if (m_Hand != null)
{
  Vector3 PalmPosition = new Vector3(0,0,0);
  Vector3 PalmNormal = new Vector3(0,0,0);
  Vector3 PalmDirection = new Vector3(0,0,0);
  if (scaled == true)
  {
    PalmPosition = m_Hand.PalmPosition.ToUnityTranslated();
    PalmNormal = m_Hand.PalmNormal.ToUnity();
    PalmDirection = m_Hand.Direction.ToUnity();
  }
  else
  {
    PalmPosition = m_Hand.PalmPosition.ToUnity();
    PalmNormal = m_Hand.PalmPosition.ToUnity();
    PalmDirection = m_Hand.Direction.ToUnity();
  }
  switch (axisName)
  {
  case "Horizontal":
    ret = PalmPosition.x ;
    break;
  case "Vertical":
    ret = PalmPosition.y;
    break;
  case "Depth":
    ret = PalmPosition.z ;
    break;
  case "Rotation":
    ret = -2 * PalmNormal.x ;
    break;
  case "Tilt":
    ret = PalmNormal.z ;
    break;
  case "HorizontalDirection":
    ret = PalmDirection.x ;
    break;
  case "VericalDirection":
    ret = PalmDirection.y ;
    break;
  default:
    break;
  }
}
if (scaled == true)
{
  if (ret > 1) {ret = 1;}
  if (ret < -1) {ret = -1;}
}
return ret;
}

I tried to make the interaction with the Leap as similar as possible as interactions that Unity has with other inputs.  So, in Unity, you would normally call Input.getAxis (axisName) to get the mouse, or arrow key buttons etc… So, I created similar

Step 8 Modify car.js script to allow or Leap Input

Ok, now for the final step.  We need to modiy the car.js script from the Racing Ttorial to allow for Leap Input.  This is where our design for GetHandAxis function will come in handy as it wil be super easy to modify car.js

1st, add a variable at the top of the script somewhere to hold a value as to whether the script is LeapEnabled (note the default s false)

// pxs modify for Leap
// store a variable to indicate whether we are using Leap as the input here
public var LeapEnabled : boolean = false;

Next find the function GetInput()

Note the 1st 2 lines in the function

throttle = Input.GetAxis("Vertical");
steer = Input.GetAxis("Horizontal");

Replace these with the following

// pxs Modify for Leap
if (LeapEnabled == true)
{
  throttle = pxsLeapInput.GetHandAxis("Depth");
  print("Throttle: " + throttle.ToString());
  steer = pxsLeapInput.GetHandAxis("Rotation");
  print("Steer: " + steer.ToString());
}
else
{
  throttle = Input.GetAxis("Vertical");
  steer = Input.GetAxis("Horizontal");
}

Run the LeapCompleteScene and note that it still works (with arrow keys etc)

Step 9 Final Step – Enable Leap Input

Select the Car in the Scene and in the car.js script component in the inspector, select the checkbox to make it Leap Enabled.

step9 

Step 10 Play

If you play the scene now, you will note that you can control the car with your hand.

Forwards and backwards is from eth hand position on top of the Leap (so towards the screen is forward for the car, away from the screen is backwards)
And the hand rotation is used to steer

step10

Enjoy