Hi guys, I'm trying to implement in c# (unity with orion) a laser ray coming from the finger. My code is this, at the moment (ignore the teleport part, i just want to show the laser for starters).
It really doesn't work, the laser position is fixed in the scene and it shoots it in vertical, any ideas why? I'm calling the functions laserActivate and laserDeactivate with fingerDirectionDetector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap.Unity;
namespace Leap.Unity {
public class LaserPointerLeap : MonoBehaviour {
public Transform cameraRigTransform;
public Transform headTransform; // The camera rig's head
public Vector3 teleportReticleOffset; // Offset from the floor for the reticle to avoid z-fighting
public LayerMask teleportMask; // Mask to filter out areas where teleports are allowed
// private SteamVR_TrackedObject trackedObj;
private Controller controller;
public GameObject laserPrefab; // The laser prefab
private GameObject laser; // A reference to the spawned laser
private Transform laserTransform; // The transform component of the laser for ease of use
public GameObject teleportReticlePrefab; // Stores a reference to the teleport reticle prefab.
private GameObject reticle; // A reference to an instance of the reticle
private Transform teleportReticleTransform; // Stores a reference to the teleport reticle transform for ease of use
private Vector3 hitPoint; // Point where the raycast hits
private bool shouldTeleport; // True if there's a valid teleport target
// private SteamVR_Controller.Device Controller
// {
// get { return SteamVR_Controller.Input((int)trackedObj.index); }
// }
//
void Awake()
{
controller = new Controller ();
// trackedObj = GetComponent<SteamVR_TrackedObject>();
}
//new
void Start()
{
laser = Instantiate(laserPrefab);
laserTransform = laser.transform;
reticle = Instantiate(teleportReticlePrefab);
teleportReticleTransform = reticle.transform;
}
public void LaserActivate(){
print ("Laser Activate");
RaycastHit hit;
Frame frame = controller.Frame ();
foreach (Hand hand in frame.Hands){
if (hand.IsRight) {
// Send out a raycast from the controller
if (Physics.Raycast (hand.GetIndex().TipPosition.Normalized.ToVector3() , transform.forward, out hit, 100, teleportMask)) {
print (hand.GetIndex ().TipPosition.Normalized.ToVector3 ());
print (hit.point);
hitPoint = hit.point;
ShowLaser (hit, frame);
//Show teleport reticle
reticle.SetActive (true);
teleportReticleTransform.position = hitPoint + teleportReticleOffset;
shouldTeleport = true;
}
// if called for more than x seconds, teleport
// if (shouldTeleport) {
// print ("shouldTeleport");
// Teleport ();
// }
}
}
}
public void LaserDeactivate(){
laser.SetActive(false);
reticle.SetActive(false);
}
private void ShowLaser(RaycastHit hit, Frame frame)
{
foreach (Hand hand in frame.Hands) {
if (hand.IsRight) {
laser.SetActive (true); //Show the laser
print (hand.GetIndex ().TipPosition.Normalized.ToVector3 ());
laserTransform.position = Vector3.Lerp (hand.GetIndex ().TipPosition.Normalized.ToVector3 (), hitPoint, .5f); // Move laser to the middle between the controller and the position the raycast hit
laserTransform.LookAt (hitPoint); // Rotate laser facing the hit point
laserTransform.localScale = new Vector3 (laserTransform.localScale.x, laserTransform.localScale.y,
hit.distance); // Scale laser so it fits exactly between the controller & the hit point
}
}
}
private void Teleport()
{
shouldTeleport = false; // Teleport in progress, no need to do it again until the next touchpad release
reticle.SetActive(false); // Hide reticle
Vector3 difference = cameraRigTransform.position - headTransform.position; // Calculate the difference between the center of the virtual room & the player's head
difference.y = 0; // Don't change the final position's y position, it should always be equal to that of the hit point
cameraRigTransform.position = hitPoint + difference; // Change the camera rig position to where the the teleport reticle was. Also add the difference so the new virtual room position is relative to the player position, allowing the player's new position to be exactly where they pointed. (see illustration)
}
}
}