Dieton

PlayerJumpState

May 18th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerJumpState : PlayerBaseState
  6. {
  7.     public PlayerJumpState(PlayerStateMachine currentContext,
  8.         PlayerStateFactory playerStateFactory)
  9.     : base(currentContext, playerStateFactory) {
  10.         IsRootState = true;
  11.     }
  12.  
  13.     public override void EnterState() {
  14.         InitializeSubState();
  15.         //Ctx.AnimCon.SetInteger(Ctx.JumpAnim, 1);
  16.         HandleJump();
  17.     }
  18.  
  19.     public override void UpdateState() {
  20.         CheckSwitchStates();
  21.         HandleGravity();
  22.     }
  23.  
  24.     public override void ExitState() {
  25.         //SwitchState(Factory.Fall());
  26.         Ctx.AnimCon.SetInteger(Ctx.JumpAnim, 4);
  27.        
  28.     }
  29.  
  30.     public override void InitializeSubState() {
  31.    
  32.     }
  33.  
  34.     public override void CheckSwitchStates() {
  35.         //SwitchState(Factory.Fall());
  36.         //Debug.Log("FallJump");
  37.         if (Ctx.PlayerController.isGrounded) {
  38.             SwitchState(Factory.Grounded());
  39.         }
  40.     }
  41.  
  42.     void HandleJump() {
  43.         //-- Jump Animation --//
  44.         //animator.SetInteger("jumpState", 1);
  45.  
  46.         Ctx.VerticalVelocity = Mathf.Sqrt(Ctx.JumpHeight * -2f * Ctx.Gravity);
  47.     }
  48.  
  49.     public void HandleGravity()
  50.     {
  51.         if (Ctx.VerticalVelocity < Ctx.TerminalVelocity)
  52.         {
  53.             Ctx.VerticalVelocity += Ctx.Gravity * Time.deltaTime;
  54.         }
  55.     }
  56. }
  57.  
Tags: Unity
Add Comment
Please, Sign In to add comment