unity3d SteamVR2.0版本利用手柄圆盘控制物体移动
1.首先看一下界面,创建一个VRController作为空物体,添加CharacterController组件,并且创建一个脚本命名为VRController,吧VR预制体拖入VRController
2.给VR相机,添加SteamVR_Camera脚本
3.点击Window窗口,点击SteamVR Input
4.绑定自己的按键(不会去百度搜)
5.绑定完成后保存一下,然后开始写代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class VRController : MonoBehaviour
{
public float m_Gravity = 30.0f;
public float m_Sensitivity = 0.1f;
public float m_MaxSpeed = 1.0f;
public float m_RotateIncrement = 90f;
public SteamVR_Action_Boolean m_RotatePress = null;
public SteamVR_Action_Boolean m_MovePress = null;
public SteamVR_Action_Vector2 m_MoveValue = null;
private float m_Speed = 0.0f;
private CharacterController m_CharacterController = null;
private Transform m_CameraRig = null;
private Transform m_Head = null;
private void Awake()
{
m_CharacterController = GetComponent<CharacterController>();
}
private void Start()
{
m_CameraRig = SteamVR_Render.Top().origin;
m_Head = SteamVR_Render.Top().head;
}
private void Update()
{
HandleHeight();
CalculateMovement();
SnapRotation();
}
private void HandleHeight()
{
float headHeight = Mathf.Clamp(m_Head.localPosition.y, 1, 2);
m_CharacterController.height = headHeight;
Vector3 newCenter = Vector3.zero;
newCenter.y = m_CharacterController.height / 2;
newCenter.y += m_CharacterController.skinWidth;
newCenter.x = m_Head.localPosition.x;
newCenter.z = m_Head.localPosition.z;
m_CharacterController.center = newCenter;
}
private void CalculateMovement()
{
Vector3 orientationEuler = new Vector3(0, m_Head.eulerAngles.y, 0);
Quaternion orientation = Quaternion.Euler(orientationEuler);
Vector3 movement = Vector3.zero;
if (m_MovePress.GetStateUp(SteamVR_Input_Sources.Any))
m_Speed = 0;
if (m_MovePress.state)
{
m_Speed += m_MoveValue.axis.y * m_Sensitivity;
m_Speed = Mathf.Clamp(m_Speed, -m_MaxSpeed, m_MaxSpeed);
movement += orientation * (m_Speed * Vector3.forward);
}
movement.y -= m_Gravity * Time.deltaTime;
m_CharacterController.Move(movement * Time.deltaTime);
}
private void SnapRotation()
{
float snapValue = 0.0f;
if (m_RotatePress.GetStateDown(SteamVR_Input_Sources.LeftHand))
snapValue = -Mathf.Abs(m_RotateIncrement);
if (m_RotatePress.GetStateDown(SteamVR_Input_Sources.RightHand))
snapValue = Mathf.Abs(m_RotateIncrement);
transform.RotateAround(m_Head.position, Vector3.up, snapValue);
}
}
总结:这是为了自己方便总结的东西,当然了 能帮到大家最好了。