holopy3/Assets/Scripts/VRController.cs

140 lines
4 KiB
C#
Raw Normal View History

2020-12-10 14:25:12 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class VRController : MonoBehaviour
{
public float Gravity = 30.0f;
public float Sensitivity = 0.1f;
public float MaxSpeed = 1.0f;
public float RotateIncrement = 90;
public SteamVR_Action_Boolean RotateLeftPress = null;
public SteamVR_Action_Boolean RotateRightPress = null;
public SteamVR_Action_Boolean MovePress = null;
public SteamVR_Action_Vector2 MoveValue = null;
private float speed = 0.0f;
private CharacterController CharacterController = null;
private Transform CameraRig = null;
private Transform Head = null;
private void Awake()
{
CharacterController = GetComponent<CharacterController>();
//MovePress = SteamVR_Actions.default_TouchpadKlick;
//MoveValue = SteamVR_Actions.default_TouchpadPosition;
}
void Start()
{
CameraRig = SteamVR_Render.Top().origin;
Head = SteamVR_Render.Top().head;
}
void Update()
{
// HandleHead();
HandleHeight();
CalculateMovement();
SnapRotation();
}
//private void HandleHead()
//{
// // Store current
// Vector3 oldPosition = CameraRig.position;
// Quaternion oldRotation = CameraRig.rotation;
// // Rotation
// transform.eulerAngles = new Vector3(0.0f, Head.rotation.eulerAngles.y, 0.0f);
// // Restore
// CameraRig.position = oldPosition;
// CameraRig.rotation = oldRotation;
//}
private void HandleHeight()
{
// Get the head in local space
float headHeight = Mathf.Clamp(Head.localPosition.y, 1, 2);
CharacterController.height = headHeight;
// Cut in half
Vector3 newCenter = Vector3.zero;
newCenter.y = CharacterController.height / 2;
newCenter.y += CharacterController.skinWidth;
// Move Capsule in local space
newCenter.x = Head.localPosition.x;
newCenter.z = Head.localPosition.z;
//// Rotate
//newCenter = Quaternion.Euler(0, -transform.eulerAngles.y, 0) * newCenter;
// Apply
CharacterController.center = newCenter;
}
private void CalculateMovement()
{
// Figure out movement orientation
Vector3 orientationEuler = new Vector3(0, Head.eulerAngles.y, 0);
Quaternion orientation = Quaternion.Euler(orientationEuler);
//Quaternion orientation = CalculateOrientation();
Vector3 movement = Vector3.zero;
// If not moving
if (MovePress.GetStateUp(SteamVR_Input_Sources.Any))
{
speed = 0;
}
// If button pressed
if (MovePress.state)
{
// Add, clamp
speed += MoveValue.axis.y * Sensitivity;
speed = Mathf.Clamp(speed, -MaxSpeed, MaxSpeed);
// Orientation
movement += orientation * (speed * Vector3.forward);
}
// Gravcity
movement.y -= Gravity * Time.deltaTime;
// Apply
CharacterController.Move(movement * Time.deltaTime);
}
//private Quaternion CalculateOrientation()
//{
// float rotation = Mathf.Atan2(MoveValue.axis.x, MoveValue.axis.y);
// rotation *= Mathf.Rad2Deg;
// Vector3 orientationEuler = new Vector3(0, Head.eulerAngles.y+rotation, 0);
// return Quaternion.Euler(orientationEuler);
//}
private void SnapRotation()
{
float snapValue = 0.0f;
if (RotateLeftPress.GetStateDown(SteamVR_Input_Sources.LeftHand) || RotateLeftPress.GetStateDown(SteamVR_Input_Sources.RightHand))
{
snapValue = -Mathf.Abs(RotateIncrement);
}
if (RotateRightPress.GetStateDown(SteamVR_Input_Sources.LeftHand) || RotateRightPress.GetStateDown(SteamVR_Input_Sources.RightHand))
{
snapValue = Mathf.Abs(RotateIncrement);
}
transform.RotateAround(Head.position, Vector3.up, snapValue);
}
}