Création d'un petit party-game anonyme de Build + Bagar sous Unity
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FreeCam.cs 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class FreeCam : MonoBehaviour
  4. {
  5. [Header("Controls")]
  6. public float moveSpeed = 10f;
  7. public float sprintSpeed = 30f;
  8. public bool ghost = true;
  9. [Header("Sensitivity")]
  10. public float XAxisSensitivity = 30f;
  11. public float YAxisSensitivity = 30f;
  12. [Space]
  13. [Range(0, 89)] public float MaxXAngle = 60f;
  14. // privates
  15. private Vector2 _inputMouse;
  16. private Vector2 _velocity;
  17. private CharacterController _controller;
  18. private float _rotationX;
  19. private bool _isSprinting;
  20. private void Start()
  21. {
  22. // TODO remove - doesn't belong here - test only
  23. Cursor.lockState = CursorLockMode.Locked;
  24. Cursor.visible = false;
  25. _inputMouse = Vector2.zero;
  26. _velocity = Vector2.zero;
  27. _controller = gameObject.GetComponent<CharacterController>();
  28. _rotationX = 0;
  29. _isSprinting = false;
  30. }
  31. // Update is called once per frame
  32. // TODO test if checking (input values = 0) to avoid calculus reduces CPU usage
  33. private void Update()
  34. {
  35. float rotationHorizontal = _inputMouse.x * XAxisSensitivity * Time.deltaTime;
  36. float rotationVertical = _inputMouse.y * YAxisSensitivity * Time.deltaTime;
  37. // always rotate Y in global world space to avoid gimbal lock
  38. transform.Rotate(Vector3.up * rotationHorizontal, Space.World);
  39. float rotationY = transform.localEulerAngles.y;
  40. _rotationX += rotationVertical;
  41. _rotationX = Mathf.Clamp(_rotationX, -MaxXAngle, MaxXAngle);
  42. transform.localEulerAngles = new Vector3(-_rotationX, rotationY, 0);
  43. float speed = _isSprinting ? sprintSpeed : moveSpeed;
  44. float translationX = _velocity.x * speed * Time.deltaTime;
  45. float translationZ = _velocity.y * speed * Time.deltaTime;
  46. Vector3 _move = new Vector3(translationX, 0f, translationZ);
  47. // ghost directly moves transform. non-ghost uses CharacterController collision detection
  48. if (ghost) {
  49. transform.Translate(_move);
  50. } else {
  51. _move = transform.TransformVector(_move);
  52. _controller.Move(_move);
  53. }
  54. }
  55. public void OnLook(InputValue value)
  56. {
  57. Vector2 valueAsVector2 = value.Get<Vector2>();
  58. _inputMouse.x = valueAsVector2.x;
  59. _inputMouse.y = valueAsVector2.y;
  60. }
  61. public void OnMovement(InputValue value)
  62. {
  63. Vector2 valueAsVector2 = value.Get<Vector2>();
  64. _velocity.x = valueAsVector2.x;
  65. _velocity.y = valueAsVector2.y;
  66. }
  67. public void OnSprint(InputValue value)
  68. {
  69. float valueAsFloat = value.Get<float>();
  70. _isSprinting = valueAsFloat == 1f;
  71. }
  72. }