手柄的控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonTouchAction : MonoBehaviour
{
public GameObject source;
public Transform pos;
SteamVR_TrackedObject trackdeObjec;
void Awake()
{
//获取手柄上的这个组件
trackdeObjec = GetComponent
}
void FixedUpdate ()
{
//获取手柄输入
SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackdeObjec.index);
if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))//按下扳机
{
GameObject go = Instantiate (source, pos.position, pos.rotation);
Rigidbody rig = go.GetComponent
rig.velocity = pos.forward * 30;
}
//按下圆盘键
if (device.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
{
GameObject obj = GameObject.Find ("Tank");
// 获取触摸板上的坐标
Vector2 pad = device.GetAxis();
// Debug.Log("按下了" + pad);
// 转换角度
Vector3 cross = Vector3.Cross(new Vector2(1, 0), pad);
float angle = Vector2.Angle(new Vector2(1, 0), pad);
float ang = cross.z > 0 ? -angle : angle;
Debug.Log(ang);
//根据角度来判断上下左右四个范围
//下
if (ang > 45 && ang < 135)
{
Debug.Log("下");
obj.transform.Translate(Vector3.back * Time.deltaTime * 5);
}
//上
else if (ang < -45 && ang> -135)
{
Debug.Log("上");
obj.transform.Translate(Vector3.forward * Time.deltaTime * 5);
}
//左
else if ((ang < 180 && ang> 135) || (ang < -135 && ang > -180))
{
Debug.Log("左");
obj.transform.Rotate(Vector3.up * Time.deltaTime * -25);
}
//右
else if ((ang > 0 && ang< 45) || (ang > -45 && ang< 0))
{
Debug.Log("右");
obj.transform.Rotate(Vector3.up * Time.deltaTime * 25);
}
}
}
}
事件的响应
using UnityEngine;
using System.Collections;
public class CatchObject : MonoBehaviour
{
//射线对象
SteamVR_LaserPointer slp;
//跟踪控制器对象
SteamVR_TrackedController stc;
//位置
Transform pointTransform;
//当前抓到物体
GameObject currentCatch;
void Start ()
{
//获取SteamVR_LaserPointer和SteamVR_TrackedController并监听属性
slp = GetComponent
print ("---"+slp);
//添加射线进入事件
slp.PointerIn += PointerIn;
//射线离开物体的事件
slp.PointerOut += PointerOut;
stc = GetComponent
if (stc == null)
{
stc = gameObject.AddComponent
}
print ("---"+stc);
//扣动扳机事件
stc.TriggerClicked += TriggerClicked;
//松开扳机事件
stc.TriggerUnclicked += TriggerUnclicked;
}
void PointerIn(object sender, PointerEventArgs e)
{
print ("PointerIn");
//判断是不是tag为Catch,如果不是就不设置标志。
if (e.target.gameObject.tag == "Catch")
{
pointTransform = e.target;
}
}
void PointerOut(object sender, PointerEventArgs e)
{
print ("PointerOut");
pointTransform = null;
}
void TriggerClicked(object sender, ClickedEventArgs e)
{
print ("TriggerClicked");
if(pointTransform == null)
{
return;
}
//修改指向物体位置,并将其绑在手柄上
pointTransform.position = this.transform.position;
pointTransform.gameObject.AddComponent
currentCatch = pointTransform.gameObject;
}
void TriggerUnclicked(object sender, ClickedEventArgs e)
{
print ("unTriggerClicked");
if (currentCatch == null)
{
return;
}
var device = SteamVR_Controller.Input((int)this.GetComponent
//使用device.TriggerHapticPulse(2500)触发手柄震动,参数为震动强度
device.TriggerHapticPulse(2500);
//松开时将速度传递给物体,实现投掷效果
currentCatch.GetComponent
currentCatch.GetComponent
Destroy(currentCatch.GetComponent
currentCatch = null;
}
}