64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.XR;
|
|
using System.Collections.Generic;
|
|
|
|
public class DroneXRController : MonoBehaviour
|
|
{
|
|
[Header("Flight Settings")]
|
|
[SerializeField] private float moveSpeed = 15f;
|
|
[SerializeField] private float rotationSpeed = 3f;
|
|
[SerializeField] private float liftSpeed = 8f;
|
|
|
|
private InputDevice rightController;
|
|
private InputDevice leftController;
|
|
|
|
void Start()
|
|
{
|
|
// Get VR controllers
|
|
var rightDevices = new List<InputDevice>();
|
|
InputDevices.GetDevicesAtXRNode(XRNode.RightHand, rightDevices);
|
|
if (rightDevices.Count > 0) rightController = rightDevices[0];
|
|
|
|
var leftDevices = new List<InputDevice>();
|
|
InputDevices.GetDevicesAtXRNode(XRNode.LeftHand, leftDevices);
|
|
if (leftDevices.Count > 0) leftController = leftDevices[0];
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
HandleDroneMovement();
|
|
HandleVerticalMovement();
|
|
HandleDroneRotation();
|
|
}
|
|
|
|
void HandleDroneMovement()
|
|
{
|
|
// Right controller thumbstick for forward/strafe movement
|
|
if (rightController.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 rightStick))
|
|
{
|
|
Vector3 moveDirection = (transform.forward * rightStick.y + transform.right * rightStick.x) * moveSpeed;
|
|
transform.position += moveDirection * Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
void HandleVerticalMovement()
|
|
{
|
|
// Left controller thumbstick Y-axis for up/down movement
|
|
if (leftController.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 leftStick))
|
|
{
|
|
Vector3 verticalMovement = transform.up * leftStick.y * liftSpeed;
|
|
transform.position += verticalMovement * Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
void HandleDroneRotation()
|
|
{
|
|
// Left controller thumbstick X-axis for yaw rotation
|
|
if (leftController.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 leftStick))
|
|
{
|
|
float yaw = leftStick.x * rotationSpeed;
|
|
transform.Rotate(0, yaw, 0, Space.Self);
|
|
}
|
|
}
|
|
}
|