Dieton

PlayerGroundedState

May 18th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerGroundedState : PlayerBaseState
  6. {
  7.     public PlayerGroundedState(PlayerStateMachine currentContext,
  8.         PlayerStateFactory playerStateFactory)
  9.     : base(currentContext, playerStateFactory) {
  10.         IsRootState = true;
  11.     }
  12.  
  13.     public void HandleGravity() {
  14.         Ctx.VerticalVelocity = -9.5f;
  15.     }
  16.  
  17.     public override void EnterState() {
  18.         //gravity
  19.         InitializeSubState();
  20.         HandleGravity();
  21.     }
  22.  
  23.     public override void UpdateState() {
  24.         CheckSwitchStates();
  25.     }
  26.  
  27.     public override void ExitState() {
  28.        
  29.     }
  30.  
  31.     public override void CheckSwitchStates()
  32.     {
  33.         //if player is grounded aloow jump
  34.         if (Ctx.InputJump)
  35.         {
  36.             SwitchState(Factory.Jump());
  37.         }
  38.         //if player is grounede allow gravity
  39.         else if (!Ctx.PlayerController.isGrounded)
  40.         {
  41.             SwitchState(Factory.Fall());
  42.         }
  43.     }
  44.  
  45.     public override void InitializeSubState() {
  46.         if (Ctx.InputMoveX == 0 && Ctx.InputMoveY == 0 && !Ctx.InputRun)
  47.         {
  48.             SetSubState(Factory.Idle());
  49.         }
  50.         else if (Ctx.InputMoveX != 0 && Ctx.InputMoveY != 0 && !Ctx.InputRun)
  51.         {
  52.             SetSubState(Factory.Walk());
  53.         }
  54.         else {
  55.             SetSubState(Factory.Run());
  56.         }
  57.     }
  58. }
  59.  
Tags: Unity
Add Comment
Please, Sign In to add comment