Hello, I am developing an application for university in Unity and I started developing it without Leap Motion in mind first. So I have an application in which I have an automatic spawn of apples and if I click on them they slice first into halves, then the halves into quarters. I need to evolve from the mouse click to the collision with the Leap Motion hand. I understood a way to do it is with the onTriggerEnter function, but I can't understand how to use it.
Here's the code
using UnityEngine;
using System.Collections;
public class SwipeTrail : MonoBehaviour {
public GameObject applePrefab;
public GameObject halfApplePrefab;
public GameObject quartApplePrefab;
bool swipeCooldown = true;
GameObject gObj = null;
// Here I have the trace of the mouse on screen, which is of course not needed for the leap hands.
Ray GenerateMouseRay (Vector3 touchPos)
{
Vector3 mousePosFar = new Vector3 (touchPos.x,
touchPos.y,
Camera.main.farClipPlane);
Vector3 mousePosNear = new Vector3 (touchPos.x,
touchPos.y,
Camera.main.nearClipPlane);
Vector3 mousePosF = Camera.main.ScreenToWorldPoint(mousePosFar);
Vector3 mousePosN = Camera.main.ScreenToWorldPoint(mousePosNear);
Ray mr = new Ray(mousePosN, mousePosF-mousePosN);
return mr;
}
// Here I have a cooldown after the first click not to cause problems with the click count.
void Cooldown ()
{
swipeCooldown = true;
}
// Here is the main part of the interaction. In this part I should substitute the mouse interaction with the Leap.
void Update () {
if ((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved)
|| Input.GetMouseButton(0))
{
Plane objPlane = new Plane (Camera.main.transform.forward * -1, this.transform.position);
Ray mRay = Camera.main.ScreenPointToRay (Input.mousePosition);
float rayDistance;
if (objPlane.Raycast (mRay, out rayDistance))
this.transform.position = mRay.GetPoint (rayDistance);
Ray mouseRay = GenerateMouseRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit) && swipeCooldown)
{
gObj = hit.transform.gameObject;
swipeCooldown = false;
Invoke ("Cooldown", 1);
if (gObj.tag == "whole") {
GameObject h1 = (GameObject)Instantiate (halfApplePrefab, gObj.transform.position, gObj.transform.rotation);
GameObject h2 = (GameObject)Instantiate (halfApplePrefab, gObj.transform.position, gObj.transform.rotation);
Destroy (gObj);
}
else if (gObj.tag == "half")
{
GameObject h1 = (GameObject)Instantiate (quartApplePrefab, gObj.transform.position, gObj.transform.rotation);
GameObject h2 = (GameObject)Instantiate (quartApplePrefab, gObj.transform.position, gObj.transform.rotation);
Destroy (gObj);
}
}
}
}
}
Thank you very much,
Alex