Advertisement
Guest User

PlayerMovement

a guest
Feb 19th, 2021
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR;
  5. using UnityEngine.XR.Interaction.Toolkit;
  6.  
  7. public class PlayerMovement : MonoBehaviour
  8. {
  9.     private InputDevice _device_leftController;
  10.     private InputDevice _device_rightController;
  11.     private CharacterController _character;
  12.     private Vector2 _inputAxis_leftController;
  13.     private Vector2 _inputAxis_rightController;
  14.     private GameObject _camera;
  15.     private float speed;
  16.     [SerializeField]
  17.     public bool snapTurningEnabled;
  18.     private bool isSnapTurning;
  19.     void Start()
  20.     {
  21.         _device_leftController = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
  22.         _device_rightController = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
  23.         _character = GetComponent<CharacterController>();
  24.         _camera = GetComponent<XRRig>().cameraGameObject;        
  25.     }
  26.     void Update()
  27.     {
  28.         _device_leftController.TryGetFeatureValue(CommonUsages.primary2DAxis, out _inputAxis_leftController);
  29.         _device_rightController.TryGetFeatureValue(CommonUsages.primary2DAxis, out _inputAxis_rightController);
  30.  
  31.         var inputVector_leftController = new Vector3(_inputAxis_leftController.x, 0, _inputAxis_leftController.y);
  32.         var inputVector_rightController = new Vector3(_inputAxis_rightController.x, 0, _inputAxis_rightController.y);
  33.  
  34.         float targetAngle = Mathf.Atan2(_inputAxis_leftController.x, _inputAxis_leftController.y) * Mathf.Rad2Deg + _camera.transform.eulerAngles.y;
  35.         var newDirection = Quaternion.Euler(0,targetAngle,0) * Vector3.forward;
  36.  
  37.         if (inputVector_leftController.magnitude>0.2)
  38.         {
  39.             speed = inputVector_leftController.magnitude * 2.0f;
  40.         }
  41.         else
  42.         {
  43.             speed = 0f;
  44.         }
  45.         if (_inputAxis_rightController.x > 0.2f || _inputAxis_rightController.x < -0.2f)
  46.         {
  47.             if (snapTurningEnabled)
  48.             {
  49.                 if (!isSnapTurning)
  50.                 {
  51.                     transform.Rotate(Vector3.up * 45f * Mathf.Sign(_inputAxis_rightController.x));
  52.                     isSnapTurning = true;
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                 transform.Rotate(Vector3.up * _inputAxis_rightController.x);
  58.             }
  59.         }
  60.         else
  61.         {
  62.             if (_inputAxis_rightController.x <= 0.2f && inputVector_rightController.x >= -0.2f)
  63.             {
  64.                 isSnapTurning = false;
  65.             }
  66.         }
  67.         _character.Move(newDirection * Time.deltaTime * speed);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement