HTC vive手柄在三维空间画线

本文介绍了一个使用VR手柄(如HTC Vive控制器)进行交互的Unity项目。通过触发手柄按钮来绘制线条,并利用LineRenderer组件实现。触摸手柄的触摸板可以清除所有已绘制的线条。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用手柄扣动扳机画线,触碰触摸板时清除线条。

1 工程设置

2 line renderer

3 Annotations.cs



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

public class Annotations : MonoBehaviour
{
	//to find HTC vive controller(Right or Left)
	public SteamVR_TrackedObject trackedObj;

	private LineRenderer line; 


	private int i = 0;  

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

	bool isNUll = true;


	// Use this for initialization
	void Start ()
	{

		line = this.GetComponent<LineRenderer>();//获得该物体上的LineRender组件 
		//line.SetColors(Color.blue, Color.red);//设置颜色  
		//line.SetWidth(0.2f, 0.1f);//设置宽度 

	}
	// Update is called once per frame
	void Update ()
	{
		if (trackedObj==null)
		{
			//print ("NULL");
			return;
		}

		SteamVR_Controller.Device device=SteamVR_Controller.Input((int)trackedObj.index);
		//SteamVR_Controller.Device device=SteamVR_Controller.Input(4);
		if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
		{

		}
		//		else if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
		else if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
		{

			//print ("GetPress");
//			Ray ray = new Ray(trackedObj.transform.position, trackedObj.transform.forward);
//			//var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//			RaycastHit hit;
//			if (GetComponent<Collider>().Raycast(ray, out hit, 50))
//			{


			var point = this.transform.position;
			print ("point"+point);

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

				RunNext = point;
			print ("RunNext"+RunNext);

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

				}

				RunStart = RunNext;

//
//			}
		}
		//touch the pad to clear lines(linerender)
		else if(device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
		{

			this.GetComponent<LineRenderer> ().positionCount = 0;
			isNUll = true;
			i = 0;


		}


	}


}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值