Advertisement
Guest User

Leap Motion Sword

a guest
May 5th, 2013
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. //Attach this script to the Right Eye of an OVRCamera.  
  2. //Then, add an empty game object as the child of the camera (local pos:0,0,0); and set its rotation rotation to 270,180,0.
  3. //Then add a cube at 0,0,0 to that gameobject (with a rigidbody) with the dimensions .1,.1,1.
  4. //Profit!!
  5. //Leap Motion wrote a bunch of this.
  6. using UnityEngine;
  7. using System.Collections;
  8. using Leap;
  9.  
  10. public class LeapSword : MonoBehaviour {
  11.     Controller controller;
  12.     public GameObject Sword;
  13.     public static float Scale = .01f;
  14.     private static Vector3 FlippedZ( Vector v ) { return new Vector3( v.x, v.y, -v.z ); }
  15.     private static Vector3 Scaled( Vector3 v ) { return new Vector3( v.x * InputScale.x, v.y * InputScale.y, v.z * InputScale.z ); }
  16.     private static Vector3 Offset( Vector3 v ) { return v + InputOffset; }
  17.     public static Vector3 InputScale = new Vector3(Scale, Scale, Scale);
  18.     public static Vector3 InputOffset = new Vector3(0,0,0);
  19.     private Vector3 LastPos = new Vector3(0,0,0);
  20.     void Start (){
  21.         controller = new Controller();
  22.     }
  23.     void Update (){
  24.         Frame frame = controller.Frame();
  25.         if(!frame.Tools.Empty){
  26.             Sword.transform.localPosition = Offset(Scaled(FlippedZ(frame.Tools.Rightmost.TipPosition)));
  27.             Sword.transform.rigidbody.velocity = (Sword.transform.position-LastPos)*100;
  28.             LastPos = Sword.transform.position;
  29.             Sword.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Scaled(FlippedZ(frame.Tools.Rightmost.Direction)));
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement