Advertisement
katubug

fixed issue with iheartgamedev yt tutorial

Jan 9th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. ======================================= Declaring Variables looks like this:
  2. // jumping variables
  3. float _initialJumpVelocity;
  4. float _maxJumpHeight;
  5. float _maxJumpTime;
  6. bool _isJumping = false;
  7. bool _isJumpAnimating = false;
  8. int _jumpCount = 0;
  9. Dictionary<int, float> _initialJumpVelocities;
  10. Dictionary<int, float> _jumpGravities;
  11. Coroutine _currentJumpResetRoutine = null;
  12.  
  13.  
  14. ====================================== Awake function looks like this:
  15. void Awake()
  16. {
  17. _initialJumpVelocities = new Dictionary<int, float>();
  18. _jumpGravities = new Dictionary<int, float>();
  19. _playerInput = new PlayerInput();
  20. _characterController = GetComponent<CharacterController>();
  21. _animator = GetComponent<Animator>();
  22.  
  23. // setup state based on this machine file, then select Grounded as default, and enter that state.
  24. _states = new PlayerStateFactory(this);
  25. _currentState = _states.Grounded();
  26. _currentState.EnterState();
  27.  
  28. _isWalkingHash = Animator.StringToHash("isWalking");
  29. _isRunningHash = Animator.StringToHash("isRunning");
  30. _isJumpingHash = Animator.StringToHash("isJumping");
  31. _jumpCountHash = Animator.StringToHash("jumpCount");
  32.  
  33. _playerInput.CharacterControls.Move.started += OnMovementInput;
  34. _playerInput.CharacterControls.Move.canceled += OnMovementInput;
  35. _playerInput.CharacterControls.Move.performed += OnMovementInput;
  36. _playerInput.CharacterControls.Run.started += OnRun;
  37. _playerInput.CharacterControls.Run.canceled += OnRun;
  38. _playerInput.CharacterControls.Jump.started += OnJump;
  39. _playerInput.CharacterControls.Jump.canceled += OnJump;
  40.  
  41. SetupJumpVariables();
  42. }
  43.  
  44. ================================================ SetupJumpVariables looks like this:
  45.  
  46. void SetupJumpVariables()
  47. {
  48. _maxJumpHeight = 2.0f;
  49. _maxJumpTime = 0.75f;
  50. float timeToApex = _maxJumpTime / 2;
  51. _gravity = (-2 * _maxJumpHeight) / (timeToApex * timeToApex);
  52. _initialJumpVelocity = (2 * _maxJumpHeight) / timeToApex;
  53.  
  54. // TODO: maybe switch out Pow as it's supposedly an expensive function
  55. float secondJumpGravity = (-2 * (_maxJumpHeight * 1.5f)) / Mathf.Pow((timeToApex * 1.25f), 2);
  56. float secondJumpInitialVelocity = (2 * (_maxJumpHeight * 1.5f) / (timeToApex * 1.25f));
  57.  
  58. // TODO: maybe switch out Pow as it's supposedly an expensive function
  59. float thirdJumpGravity = (-2 * (_maxJumpHeight * 2f)) / Mathf.Pow((timeToApex * 1.5f), 2);
  60. float thirdJumpInitialVelocity = (2 * (_maxJumpHeight * 2f) / (timeToApex * 1.5f));
  61.  
  62. _initialJumpVelocities.Add(1, _initialJumpVelocity);
  63. _initialJumpVelocities.Add(2, secondJumpInitialVelocity);
  64. _initialJumpVelocities.Add(3, thirdJumpInitialVelocity);
  65.  
  66. _jumpGravities.Add(0, _gravity);
  67. _jumpGravities.Add(1, _gravity);
  68. _jumpGravities.Add(2, secondJumpGravity);
  69. _jumpGravities.Add(3, thirdJumpGravity);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement