11using UnityEngine ;
22using WebXR ;
33
4+ [ RequireComponent ( typeof ( Camera ) ) ]
45public class DesertFreeFlightController : MonoBehaviour {
56 [ Tooltip ( "Enable/disable rotation control. For use in Unity editor only." ) ]
67 public bool rotationEnabled = true ;
@@ -27,11 +28,17 @@ public class DesertFreeFlightController : MonoBehaviour {
2728
2829 Quaternion originalRotation ;
2930
31+ private Camera attachedCamera ;
32+ private Vector3 axis ;
33+ private Vector3 axisLastFrame ;
34+ private Vector3 axisDelta ;
35+
3036 void Start ( )
3137 {
3238 WebXRManager . Instance . OnXRChange += onXRChange ;
3339 WebXRManager . Instance . OnXRCapabilitiesUpdate += onXRCapabilitiesUpdate ;
3440 originalRotation = transform . localRotation ;
41+ attachedCamera = GetComponent < Camera > ( ) ;
3542 }
3643
3744 private void onXRChange ( WebXRState state )
@@ -61,11 +68,20 @@ void Update() {
6168 transform . Translate ( x , 0 , z ) ;
6269 }
6370
64- if ( rotationEnabled && Input . GetMouseButton ( 0 ) )
71+ if ( rotationEnabled )
6572 {
66-
67- rotationX += Input . GetAxis ( "Mouse X" ) * mouseSensitivity ;
68- rotationY += Input . GetAxis ( "Mouse Y" ) * mouseSensitivity ;
73+ if ( Input . GetMouseButtonDown ( 0 ) )
74+ {
75+ axisLastFrame = attachedCamera . ScreenToViewportPoint ( Input . mousePosition ) ;
76+ }
77+ if ( Input . GetMouseButton ( 0 ) )
78+ {
79+ axis = attachedCamera . ScreenToViewportPoint ( Input . mousePosition ) ;
80+ axisDelta = ( axisLastFrame - axis ) * 90f ;
81+ axisLastFrame = axis ;
82+
83+ rotationX += axisDelta . x * mouseSensitivity ;
84+ rotationY += axisDelta . y * mouseSensitivity ;
6985
7086 rotationX = ClampAngle ( rotationX , minimumX , maximumX ) ;
7187 rotationY = ClampAngle ( rotationY , minimumY , maximumY ) ;
@@ -74,6 +90,7 @@ void Update() {
7490 Quaternion yQuaternion = Quaternion . AngleAxis ( rotationY , Vector3 . left ) ;
7591
7692 transform . localRotation = originalRotation * xQuaternion * yQuaternion ;
93+ }
7794 }
7895 }
7996
0 commit comments