Advertisement
DugganSC

Player.cs

Jan 6th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3.  
  4. public class Player : MonoBehaviour
  5. {
  6. private CharacterController _controller;
  7. private PlayerControls _playerInputs;
  8.  
  9. [SerializeField]
  10. private float _moveSpeed = 5f;
  11. [SerializeField]
  12. private float _gravity = 10f;
  13. [SerializeField]
  14. private float _jumpHeight = 50f;
  15.  
  16. private bool _isJumping;
  17.  
  18. [SerializeField]
  19. private float _yVelocity;
  20.  
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. _controller = GetComponent<CharacterController>();
  25. _playerInputs = new PlayerControls();
  26. _playerInputs.Platforming.Enable();
  27. _playerInputs.Platforming.Jump.performed += Jump_performed;
  28. }
  29.  
  30. private void Jump_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  31. {
  32. if (_controller.isGrounded)
  33. {
  34. _isJumping = true;
  35. }
  36. }
  37.  
  38. // Update is called once per frame
  39. void Update()
  40. {
  41. Vector2 playerMovement = _playerInputs.Platforming.Movement.ReadValue<Vector2>();
  42. playerMovement.Normalize();
  43.  
  44. Vector3 velocity = new Vector3(playerMovement.x, 0, 0) * _moveSpeed;
  45.  
  46. if (!_controller.isGrounded)
  47. {
  48. _yVelocity -= _gravity;
  49. }
  50. else
  51. {
  52. if (_isJumping)
  53. {
  54. _yVelocity = _jumpHeight;
  55. _isJumping = false;
  56. }
  57. else
  58. {
  59. _yVelocity = 0;
  60. }
  61. }
  62.  
  63. velocity.y = _yVelocity;
  64.  
  65. _controller.Move(velocity * Time.deltaTime);
  66. }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement