I am trying to create an aiming system in Unity, where index finger is being used to control a crosshair. The game is being played with Oculus and Leap Motion is mounted on to the Oculus headset.
I have tried to calculate the position for the crosshair based on the angle of the index finger and draw a ray based on a given distance. I have also tried calculating the crosshair location just based on the direction of the index finger and given distance like this:
Vector3 targetPoint = direction * direction;
Here is what I have tried:
void Start () {
crossHair = GameObject.Find("Crosshair Component");
myCamera = GameObject.Find("CenterEyeAnchor");
target = crossHair.transform;
controller = new Controller();
indexFinger = new Finger();
void Update () {
crossHair.transform.position = myCamera.transform.position;
frame = controller.Frame();
List<Hand> handList = new List<Hand>();
for (int h = 0; h < frame.Hands.Count; h++)
{
Hand leapHand = frame.Hands[h];
handList.Add(leapHand);
}
if (handList != null && frame.Hands.Count > 0) {
indexFinger = frame.Hands[0].Fingers[(int)Finger.FingerType.TYPE_INDEX];
if (indexFinger.IsExtended)
{
Vector3 fingerTipPos = indexFinger.TipPosition.ToUnityScaled();
Vector3 originAngle = indexFinger.TipPosition.ToUnityScaled();
Vector3 targetAngle = crossHair.transform.position;
float distance = 1;
Vector3 direction = indexFinger.Direction.ToUnityScaled();
Ray rayToTest = new Ray(originAngle, targetAngle);
Vector3 targetPoint = rayToTest.GetPoint(distance);
//Problem is here. How should these targetPoint values be used to calculate correct position for the crosshair?
crossHair.transform.position = new Vector3(crossHair.transform.position.x + (targetPoint.x), y,
crossHair.transform.position.z + (targetPoint.z));
}
}
}
}
With similar calculations as these I have been able to move the crosshair accordingly on horizontal level by modifying x and z values but the y-axis seems to be the biggest problem. Y-axis seems to always receive too low values. The crosshair element is placed as a child element for the CenterEyeAnchor camera object in Unity.
So questions are: Is the way of calculating the target point position right as I have been trying to make it? What kind of calculations would I have to make for the crosshairs position based on the new target point value to make it behave accordingly to the index finger movement? Scaling factors for the values?