版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/itsxwz/article/details/82141992
1.AssetStore搜索下载以下package
- VRTK - Virtual Reality Toolkit - [VR Toolkit]
- SteamVR
- VIVE Input Utility
2.得到[VRTK_SDKManager]
- 根据路径Assets/VRTK/Examples/001_CameraRig_VRPlayArea,打开该场景
- 找到[VRTK_SDKManager],拖拽到Project视图为预制件
3.创建右(左)手柄物体
- 在需要的场景把[VRTK_SDKManager]预制件拖入
- [VRTK_SDKManager]上设置左右手柄
- SteamVR位置的Y轴归零
- [CameraRig]上设置左右手柄
- [CameraRig]下,创建GameObject,添加Steam VR_Tracked Object 脚本,Index设置为Device 1
4.脚本(物体跟随上一步骤创建的手柄移动)
public class FollowHandShank: MonoBehaviour
{
#region--Define Variables
private SteamVR_TrackedObject hand;
private SteamVR_Controller.Device device;
public GameObject handShank;//手柄(步骤三的GameObject)
public GameObject player;//跟随的物体
#endregion
#region--System Functions
void Start()
{
hand = GetComponentInParent<SteamVR_TrackedObject>();
device = SteamVR_Controller.Input((int)hand.index);
//设置手柄为父物体 并跟随手柄移动
if (handShank != null)
{
player.transform.SetParent(handShank.transform);
player.transform.localPosition = new Vector3(0, 0, 0);
}
}
void Update()
{
//叩击手柄控制物体状态
PlayerStateControll();
}
#endregion
#region--Define Functions
public void PlayerStateControll()
{
if (hand.isValid)
{
hand = GetComponentInParent<SteamVR_TrackedObject>();
}
device = SteamVR_Controller.Input((int)hand.index);
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
//Do something
}
}
#endregion
}