steamVR的通过代码实现简单操作

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 23:57   11   0

左手的控制

usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
publicclassleftss:MonoBehaviour{
SteamVR_TrackedObjecttrackedObj;
SteamVR_Controller.Devicedevice;
publicTransformsphere;//重置功能
voidAwake(){
trackedObj=GetComponent<SteamVR_TrackedObject>();
}
voidFixedUpdate(){
device=SteamVR_Controller.Input((int)trackedObj.index);
if(device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)){
Debug.Log("TouchTrigger");
}
if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)){
Debug.Log("TouchDownTrigger");
}
if(device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)){
Debug.Log("TouchUpTrigger");
}
//把物体拖入实现重置功能
if(device.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad)){
sphere.transform.position=Vector3.zero;
sphere.GetComponent<Rigidbody>().velocity=Vector3.zero;
sphere.GetComponent<Rigidbody>().angularVelocity=Vector3.zero;
}
}
voidOnTriggerStay(Collidercollider)
{
//物品抓取
if(device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)){
collider.attachedRigidbody.isKinematic=true;
collider.gameObject.transform.SetParent(gameObject.transform);
}
//物品分离
if(device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)){
collider.attachedRigidbody.isKinematic=false;
collider.gameObject.transform.SetParent(null);
tossObject(collider.attachedRigidbody);
}
}
//物品投掷
voidtossObject(Rigidbodyrigidbody)
{
Transformorgin=trackedObj.origin?trackedObj.origin:trackedObj.transform.parent;
if(orgin!=null){
rigidbody.velocity=orgin.TransformVector((device.velocity));
rigidbody.angularVelocity=orgin.TransformVector(device.angularVelocity);
}else
{
rigidbody.velocity=device.velocity;
rigidbody.angularVelocity=device.angularVelocity;
}
//rigidbody.velocity=device.velocity;
//rigidbody.angularVelocity=device.angularVelocity;

}
}

右手的控制


usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
publicclassrightss:MonoBehaviour{
//追踪的设备,就是我们的手柄
SteamVR_TrackedObjecttrackedObj;
//获取手柄的输入
SteamVR_Controller.Devicedevice;
//固定关节
FixedJointfixedJoint;
//位于手柄上的刚体
publicRigidbodyrigidBodyAttachPoint;
publicTransformsphere;
voidAwake(){
trackedObj=GetComponent<SteamVR_TrackedObject>();
}
voidFixedUpdate(){
device=SteamVR_Controller.Input((int)trackedObj.index);
//重置功能
if(device.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad)){
//位置
sphere.transform.position=Vector3.zero;
//速度
sphere.GetComponent<Rigidbody>().velocity=Vector3.zero;
//角速度
sphere.GetComponent<Rigidbody>().angularVelocity=Vector3.zero;
}
}
voidOnTriggerStay(Collidercollider)
{
if(fixedJoint==null&&device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)){
//把关节组件添加到实例化对象上,连接的为位置就是这个刚体
fixedJoint=collider.gameObject.AddComponent<FixedJoint>();
fixedJoint.connectedBody=rigidBodyAttachPoint;
}elseif(fixedJoint!=null&&device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)){
//获取关节上的游戏对象
GameObjectgo=fixedJoint.gameObject;
//获取刚体
Rigidbodyrigidbody=go.GetComponent<Rigidbody>();
//销毁
Object.Destroy(fixedJoint);
//设置为空
fixedJoint=null;
tossObject(rigidbody);
}
}
voidtossObject(Rigidbodyrigidbody)
{
Transformorgin=trackedObj.origin?trackedObj.origin:trackedObj.transform.parent;
if(orgin!=null){
rigidbody.velocity=orgin.TransformVector((device.velocity));
rigidbody.angularVelocity=orgin.TransformVector(device.angularVelocity);
}else{
rigidbody.velocity=device.velocity;
rigidbody.angularVelocity=device.angularVelocity;
}
}
}



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP