Leap手心发射线,碰撞点用小球表示,并用Line Renderer画出来_知识

Leap手心发射线,碰撞点用小球表示,并用Line Renderer画出来_ide_02

1 食指和大拇指的距离比较近同时握力较大时清楚射线和标记.

2 红色小球的碰撞器要去掉,不然小球碰到有碰撞器的物体会有弹回的效果。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Leap;
using Leap.Unity;

// PlanA 	cube
//PlanB plamnormal



public class beam : MonoBehaviour {

	private LeapProvider mProvider;
	Frame mFrame;
	Hand mHand;

	public float thickness = 0.002f;
	public float thicknessIn = 0.008f;
	public GameObject pointer;

	private LineRenderer line; 
	private int i = 0;  

	Vector3 RunStart = Vector3.zero;
	Vector3 RunNext = Vector3.zero;

	bool isNUll = true;

	//beam pos and direction
	Vector3 pos = Vector3.zero;
	Vector3 dir = Vector3.zero;

	//finger tip
	GameObject tip;
	GameObject move;


	// Use this for initialization
	void Start () {
		mProvider = FindObjectOfType<LeapProvider> () as LeapProvider;

	
		pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
		pointer.transform.parent = this.transform;
		pointer.transform.localScale = new Vector3(0.000f, 0.000f, 10.0f);
		pointer.transform.position = this.transform.position;
		pointer.transform.rotation = this.transform.rotation;


		 tip = GameObject.Find("tip");

		line = this.GetComponent<LineRenderer>();//获得该物体上的LineRender组件 
	
	}
	
	// Update is called once per frame
	void Update () {


		mFrame = mProvider.CurrentFixedFrame;
		int numHands = mFrame.Hands.Count;

		if (numHands >0) {

			if (numHands == 1) {


				foreach (var itemFingers in mFrame.Hands) {
					int numF =	itemFingers.Fingers.Count;

					float dis =	itemFingers.PinchDistance;
					float disN = itemFingers.PinchStrength;


				

					if (dis < 50.0f || disN > 0.4f) {
						pointer.transform.localScale = new Vector3 (0.000f, 0.000f, 10.0f);
						this.GetComponent<LineRenderer> ().positionCount = 0;
						isNUll = true;
						i = 0;


					} else {

						pointer.transform.localScale = new Vector3(0.0008f, 0.0008f, 10.0f);

						Ray ray = new Ray (transform.position, -transform.forward);
						//var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
						Debug.DrawLine(transform.position, -transform.forward*100,Color.red);  //这个就是绘制出的射线了,包含发射位置,发射距离和射线的颜色; 

						RaycastHit hit;

						if (Physics.Raycast (ray, out hit, 100)) { 
							//print ("hello");
							//print (hit.point);
							//hit.point;
							tip.transform.position = hit.point;

							var point = hit.point;

							//To have lines using LineRenderer
							if(isNUll)
							{
								RunStart = point;
								isNUll = false;
							}

							RunNext = point;

							if (RunStart != RunNext) {
								i++;
								line.SetVertexCount(i);//设置顶点数 
								line.SetPosition(i-1, point);

							}

							RunStart = RunNext;

					
						}





					}
				
					//print (numF);
					//itemFingers.PalmNormal
				}
				//print ("Find one hand");
				//pointer.transform.position.z = pointer.transform.position.z + 1.0f;
			}

			if (numHands == 2) {

				//print ("Find two hands");

			}
		}
			
	}
}