PlayerController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. using System.Linq;
  4. namespace Unity.RenderStreaming.Samples
  5. {
  6. class PlayerController : MonoBehaviour
  7. {
  8. [SerializeField] GameObject player;
  9. [SerializeField] GameObject cameraPivot;
  10. [SerializeField] InputReceiver playerInput;
  11. [SerializeField] TextMesh label;
  12. [SerializeField] GameObject captionForMobile;
  13. [SerializeField] GameObject captionForDesktop;
  14. [SerializeField] float moveSpeed = 100f;
  15. [SerializeField] float rotateSpeed = 10f;
  16. [SerializeField] float jumpSpeed = 500f;
  17. const float CooldownJump = 1.2f; // second
  18. Vector2 inputMovement;
  19. Vector2 inputLook;
  20. Vector3 initialPosition;
  21. bool inputJump;
  22. float cooldownJumpDelta = CooldownJump;
  23. protected void Awake()
  24. {
  25. playerInput.onDeviceChange += OnDeviceChange;
  26. initialPosition = transform.position;
  27. }
  28. void OnDeviceChange(InputDevice device, InputDeviceChange change)
  29. {
  30. switch (change)
  31. {
  32. case InputDeviceChange.Added:
  33. {
  34. playerInput.PerformPairingWithDevice(device);
  35. CheckPairedDevices();
  36. return;
  37. }
  38. case InputDeviceChange.Removed:
  39. {
  40. playerInput.UnpairDevices(device);
  41. CheckPairedDevices();
  42. return;
  43. }
  44. }
  45. }
  46. public void CheckPairedDevices()
  47. {
  48. if (!playerInput.user.valid)
  49. return;
  50. bool hasTouchscreenDevice =
  51. playerInput.user.pairedDevices.Count(_ => _.path.Contains("Touchscreen")) > 0;
  52. captionForMobile.SetActive(hasTouchscreenDevice);
  53. captionForDesktop.SetActive(!hasTouchscreenDevice);
  54. }
  55. private void Update()
  56. {
  57. var forwardDirection = Quaternion.Euler(0, cameraPivot.transform.eulerAngles.y, 0);
  58. var moveForward = forwardDirection * new Vector3(inputMovement.x, 0, inputMovement.y);
  59. player.GetComponent<Rigidbody>().AddForce(moveForward * Time.deltaTime * moveSpeed);
  60. var moveAngles = new Vector3(-inputLook.y, inputLook.x);
  61. var newAngles = cameraPivot.transform.localEulerAngles + moveAngles * Time.deltaTime * rotateSpeed;
  62. cameraPivot.transform.localEulerAngles = new Vector3(Mathf.Clamp(newAngles.x, 0, 45), newAngles.y, 0);
  63. if (inputJump && cooldownJumpDelta <= 0.0f)
  64. {
  65. var jumpForward = forwardDirection * new Vector3(0, 1f, 0);
  66. player.GetComponent<Rigidbody>().AddForce(jumpForward * jumpSpeed);
  67. cooldownJumpDelta = CooldownJump;
  68. }
  69. // jump cooldown
  70. if (cooldownJumpDelta >= 0.0f)
  71. {
  72. inputJump = false;
  73. cooldownJumpDelta -= Time.deltaTime;
  74. }
  75. // reset if the ball fall down from the floor
  76. if (player.transform.position.y < -5)
  77. {
  78. player.transform.position = initialPosition;
  79. player.GetComponent<Rigidbody>().velocity = Vector3.zero;
  80. }
  81. }
  82. public void SetLabel(string text)
  83. {
  84. label.text = text;
  85. }
  86. public void OnControlsChanged()
  87. {
  88. }
  89. public void OnDeviceLost()
  90. {
  91. }
  92. public void OnDeviceRegained()
  93. {
  94. }
  95. public void OnMovement(InputAction.CallbackContext value)
  96. {
  97. inputMovement = value.ReadValue<Vector2>();
  98. }
  99. public void OnLook(InputAction.CallbackContext value)
  100. {
  101. inputLook = value.ReadValue<Vector2>();
  102. }
  103. public void OnJump(InputAction.CallbackContext value)
  104. {
  105. if (value.performed)
  106. {
  107. inputJump = true;
  108. }
  109. }
  110. }
  111. }