JojikYT

JumpingState

Dec 18th, 2021 (edited)
3,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class JumpingState:State
  4. {
  5.     bool grounded;
  6.  
  7.     float gravityValue;
  8.     float jumpHeight;
  9.     float playerSpeed;
  10.  
  11.     Vector3 airVelocity;
  12.  
  13.     public JumpingState(Character _character, StateMachine _stateMachine) : base(_character, _stateMachine)
  14.     {
  15.         character = _character;
  16.         stateMachine = _stateMachine;
  17.     }
  18.  
  19.     public override void Enter()
  20.     {
  21.         base.Enter();
  22.  
  23.         grounded = false;
  24.         gravityValue = character.gravityValue;
  25.         jumpHeight = character.jumpHeight;
  26.         playerSpeed = character.playerSpeed;
  27.         gravityVelocity.y = 0;
  28.  
  29.         character.animator.SetFloat("speed", 0);
  30.         character.animator.SetTrigger("jump");
  31.         Jump();
  32.     }
  33.     public override void HandleInput()
  34.     {
  35.         base.HandleInput();
  36.  
  37.         input = moveAction.ReadValue<Vector2>();
  38.     }
  39.  
  40.     public override void LogicUpdate()
  41.     {
  42.         base.LogicUpdate();
  43.  
  44.         if (grounded)
  45.         {
  46.             stateMachine.ChangeState(character.landing);
  47.         }
  48.     }
  49.  
  50.     public override void PhysicsUpdate()
  51.     {
  52.         base.PhysicsUpdate();
  53.         if (!grounded)
  54.         {
  55.  
  56.             velocity = character.playerVelocity;
  57.             airVelocity = new Vector3(input.x, 0, input.y);
  58.  
  59.             velocity = velocity.x * character.cameraTransform.right.normalized + velocity.z * character.cameraTransform.forward.normalized;
  60.             velocity.y = 0f;
  61.             airVelocity = airVelocity.x * character.cameraTransform.right.normalized + airVelocity.z * character.cameraTransform.forward.normalized;
  62.             airVelocity.y = 0f;
  63.             character.controller.Move(gravityVelocity * Time.deltaTime+ (airVelocity*character.airControl+velocity*(1- character.airControl))*playerSpeed*Time.deltaTime);
  64.         }
  65.  
  66.         gravityVelocity.y += gravityValue * Time.deltaTime;
  67.         grounded = character.controller.isGrounded;
  68.     }
  69.  
  70.     void Jump()
  71.     {
  72.         gravityVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
  73.     }
  74.  
  75. }
  76.  
  77.  
Add Comment
Please, Sign In to add comment