JojikYT

StandingState

Dec 18th, 2021 (edited)
2,976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class StandingState: State
  4. {
  5.    
  6.     float gravityValue;
  7.     bool jump;  
  8.     bool crouch;
  9.     Vector3 currentVelocity;
  10.     bool grounded;
  11.     bool sprint;
  12.     float playerSpeed;
  13.  
  14.     Vector3 cVelocity;
  15.  
  16.     public StandingState(Character _character, StateMachine _stateMachine) : base(_character, _stateMachine)
  17.     {
  18.         character = _character;
  19.         stateMachine = _stateMachine;
  20.     }
  21.  
  22.     public override void Enter()
  23.     {
  24.         base.Enter();
  25.  
  26.         jump = false;
  27.         crouch = false;
  28.         sprint = false;
  29.         input = Vector2.zero;
  30.         velocity = Vector3.zero;
  31.         currentVelocity = Vector3.zero;
  32.         gravityVelocity.y = 0;
  33.  
  34.         playerSpeed = character.playerSpeed;
  35.         grounded = character.controller.isGrounded;
  36.         gravityValue = character.gravityValue;    
  37.     }
  38.  
  39.     public override void HandleInput()
  40.     {
  41.         base.HandleInput();
  42.  
  43.         if (jumpAction.triggered)
  44.         {
  45.             jump = true;
  46.         }
  47.         if (crouchAction.triggered)
  48.         {
  49.             crouch = true;
  50.         }
  51.         if (sprintAction.triggered)
  52.         {
  53.             sprint = true;
  54.         }
  55.  
  56.         input = moveAction.ReadValue<Vector2>();
  57.         velocity = 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.      
  62.     }
  63.  
  64.     public override void LogicUpdate()
  65.     {
  66.         base.LogicUpdate();
  67.  
  68.         character.animator.SetFloat("speed", input.magnitude, character.speedDampTime, Time.deltaTime);
  69.  
  70.         if (sprint)
  71.         {
  72.             stateMachine.ChangeState(character.sprinting);
  73.         }    
  74.         if (jump)
  75.         {
  76.             stateMachine.ChangeState(character.jumping);
  77.         }
  78.         if (crouch)
  79.         {
  80.             stateMachine.ChangeState(character.crouching);
  81.         }
  82.        
  83.     }
  84.  
  85.     public override void PhysicsUpdate()
  86.     {
  87.         base.PhysicsUpdate();
  88.  
  89.         gravityVelocity.y += gravityValue * Time.deltaTime;
  90.         grounded = character.controller.isGrounded;
  91.  
  92.         if (grounded && gravityVelocity.y < 0)
  93.         {
  94.             gravityVelocity.y = 0f;
  95.         }
  96.        
  97.         currentVelocity = Vector3.SmoothDamp(currentVelocity, velocity,ref cVelocity, character.velocityDampTime);
  98.         character.controller.Move(currentVelocity * Time.deltaTime * playerSpeed + gravityVelocity * Time.deltaTime);
  99.  
  100.         if (velocity.sqrMagnitude>0)
  101.         {
  102.             character.transform.rotation = Quaternion.Slerp(character.transform.rotation, Quaternion.LookRotation(velocity),character.rotationDampTime);
  103.         }
  104.        
  105.     }
  106.  
  107.     public override void Exit()
  108.     {
  109.         base.Exit();
  110.  
  111.         gravityVelocity.y = 0f;
  112.         character.playerVelocity = new Vector3(input.x, 0, input.y);
  113.  
  114.         if (velocity.sqrMagnitude > 0)
  115.         {
  116.             character.transform.rotation = Quaternion.LookRotation(velocity);
  117.         }
  118.     }
  119.  
  120. }
  121.  
Add Comment
Please, Sign In to add comment