Htc vive Unity 新教程
晋中职业技术学院 智祥明
我使用的Unity版本为Unity 2017.4.40c1;SteamVR版本为1.15.12 ;SteamVR Plugin 为2.6.1(sdk 1.13.10)
所做案例为用手柄将Cube 拿起来。
1、 导入SteamVR Plugin
2、 将预制体[CameraRig] 和 [SteamVR] 拖放到Hierachy面板中
3、 创建一个Cube , 给Cube加一个刚体Rigidbody,下面创建一个Plane
4、 Cube的Box Collider 勾选 Is Trigger,用于碰撞触发。(碰撞触发的条件:双方要有Collider;一方要有刚体Rigidbody;一方要勾选Is Trigger)
5、 将Cube的Use Gravity 去选,如果选上,因勾选了Box Collider 勾选 Is Trigger,Cube就会下落,并穿过Plane。
6、 将Cube的Is Kinematic 去选。如勾选,其就不受物理引擎驱动,那么,FixedJoint固定链接就不起作用,Cube就拿不起来。
7、 手柄Controller(left)和Controller(right)默认就添加脚本Steam VR_Behaviour_Pose,我们给两个手柄添加刚体Rigidbody,将Is Kinematic 勾选;Use Gravity去选,不让手柄受重力影响。
8、 给两个手柄添加 Box Collider,Center 坐标为 X: 0;Y:-0.04;Z:0.02 目的是让Collider放置在手柄的头部位置,碰撞及操作更方便自然。
9、 创建脚本,挂载在两个手柄上。
10、 引入命名空间
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
11、 声明变量 public SteamVR_Behaviour_Pose pose;在Inspector面板中分别赋值为
Cotroller(left)(SteamVR_Behaviour_Pose)、Cotroller(right)(SteamVR_Behaviour_Pose)
获取两个手柄的SteamVR_Behaviour_Pose组件。
12、 声明变量private GameObject collidingObject; 用于保存碰撞到的刚体对象
13、 声明变量private GameObject objectInHand;用于保存手柄链接的对象
14、 声明变量public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction(“InteractUI”);
用于保存手柄的行为。在Inspector面板中可选。选择“\actions\default\in\InteractUI”时,为手柄按钮的状态。
15、 声明变量 FixedJoint joint;用于存放“固定关节”变量。
16、创建方法:
private void SetCollidingObject(Collider col)
{
// 1
if (collidingObject || !col.GetComponent())
{
return;
}
// 2
collidingObject = col.gameObject;
}
这个方法接受一个碰撞体作为参数,并将它的 GameObject 保存到 collidingObject 变量,以便抓住和释放这个对象。同时:
- 如果玩家已经抓着某些东西了,或者这个对象没有一个刚性体,则不要将这个 GameObject 作为可以抓取目标。
- 将这个对象作为可以抓取的目标。
17、现在,添加触发器方法:
// 1
public void OnTriggerEnter(Collider other)
{
SetCollidingObject(other);
}
// 2
public void OnTriggerStay(Collider other)
{
SetCollidingObject(other);
}
// 3
public void OnTriggerExit(Collider other)
{
if (!collidingObject)
{
return;
}
collidingObject = null;
}
当触发器碰撞体进入、退出另一个碰撞体时,这些方法将被触发。
- 当触发器碰撞体进入另一个碰撞体时,将另一个碰撞体作为可以抓取的目标。
- 和第一段类似(第一段注释 //1),但不同的是玩家已经将手柄放在一个对象上并持续一段时间。如果没有这段代码,碰撞会失败或者会导致异常。
- 当碰撞体退出一个对象,放弃目标,这段代码会将 collidingObject 设为 null 以删除目标对象。
17、下面的代码用于抓住一个对象:
private void GrabObject()
{
// 1
objectInHand = collidingObject;
collidingObject = null;
// 2
joint = AddFixedJoint();//将AddComponent <FixedJoint >()的返回值保存“固定关节”类型变量,保存到joint中。
print(joint);
joint.connectedBody = gameObject .GetComponent<Rigidbody>();//将手柄的刚体组件作为“固定关节”链接的对象
print(joint);
}
private FixedJoint AddFixedJoint()//此方法返回FixedJiont(固定关节)类型,用于和某个游戏对象链接,连接两个没有父子关系的对象使其一起运动,使用固定关节的对象自身需要有一个刚体组件。
{
joint = objectInHand .AddComponent <FixedJoint >();//给手柄增加FixedJiont(固定关节)组件,赋给joint
joint.breakForce = 20000;
joint .breakTorque = 20000;
return joint ;
}
在这里,我们:
- 在玩家手中的 GameObject 转移到 objectInHand 中,将 collidingObject 中保存的 GameObject 移除。
- 添加一个连接对象,调用下面的 FixedJoint 方法将手柄和 GameObject 连接起来。
- 创建一个固定连接并加到手柄中,并设置连接属性,使它坚固,不那么容易断裂。最后返回这个连接。
18、最后,在 Update() 方法中添加代码以处理手柄的输入:
if (collidingObject && teleport.GetStateDown(pose.inputSource))//如果按下按钮
{
GrabObject();
Debug.LogError(“按下了”);
//if (mOnClick != null) mOnClick.Invoke();
}
就可以抓取物体了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
//[RequireComponent(typeof(SteamVR_TrackedObject))]
public class VRInput_1 : MonoBehaviour {
public SteamVR_Behaviour_Pose pose;//获取两个手柄
// 1
private GameObject collidingObject;//创建游戏对象类型,用于保存碰撞到的刚体对象。在触发事件中调用SetCollidingObject(Collider col)方法实现。
// 2
private GameObject objectInHand;//将碰撞到的刚体对象作为链接对象,保存起来。
public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction("InteractUI");//检测按下手柄的那个按键,保存在teleport中。
FixedJoint joint;//声明“固定关节”类型变量,
//void Awake()
//{
// pose= GetComponent<SteamVR_Behaviour_Pose>();
//}
private void SetCollidingObject(Collider col)
{
// 1
if (collidingObject || !col.GetComponent<Rigidbody>())
{
return;
}
// 2
collidingObject = col.gameObject;
}
// 1
public void OnTriggerEnter(Collider other)
{
SetCollidingObject(other);
}
// 2
public void OnTriggerStay(Collider other)
{
SetCollidingObject(other);
}
// 3
public void OnTriggerExit(Collider other)
{
if (!collidingObject)
{
return;
}
collidingObject = null;
}
private void GrabObject()
{
// 1
objectInHand = collidingObject;
collidingObject = null;
// 2
joint = AddFixedJoint();//将AddComponent <FixedJoint >()的返回值保存“固定关节”类型变量,保存到joint中。
print(joint);
joint.connectedBody = gameObject .GetComponent<Rigidbody>();//将手柄的刚体组件作为“固定关节”链接的对象
print(joint);
}
private FixedJoint AddFixedJoint()//此方法返回FixedJiont(固定关节)类型,用于和某个游戏对象链接,连接两个没有父子关系的对象使其一起运动,使用固定关节的对象自身需要有一个刚体组件。
{
joint = objectInHand .AddComponent <FixedJoint >();//给手柄增加FixedJiont(固定关节)组件,赋给joint
joint.breakForce = 20000;
joint .breakTorque = 20000;
return joint ;
}
private void ReleaseObject()
{
// 1
if (objectInHand.GetComponent<FixedJoint>())
{
// 2
objectInHand.GetComponent<FixedJoint>().connectedBody = null;//将FixedJiont(固定关节)组件的链接刚体属性置为空
Object.DestroyImmediate(joint);//销毁FixedJiont(固定关节)组件
// 3
objectInHand.GetComponent<Rigidbody>().velocity = pose .GetVelocity();
objectInHand.GetComponent<Rigidbody>().angularVelocity = pose .GetAngularVelocity();
}
// 4
objectInHand = null;
}
// public UnityEvent mOnClick = null;
void Update()
{
if (collidingObject && teleport.GetStateDown(pose.inputSource))//如果按下按钮
{
GrabObject();
Debug.LogError("按下了");
//if (mOnClick != null) mOnClick.Invoke();
}
//if (objectInHand && teleport.GetStateDown(pose.inputSource))
//{
// ReleaseObject();
// Debug.LogError("按下了");
// //if (mOnClick != null) mOnClick.Invoke();
//}
}
}