JojikYT

StandingState

Feb 10th, 2022
3,997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class StandingState: State
  4. {  
  5.     float gravityValue;
  6.     bool jump;  
  7.     bool crouch;
  8.     Vector3 currentVelocity;
  9.     bool grounded;
  10.     bool sprint;
  11.     float playerSpeed;
  12.     bool drawWeapon;
  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.         drawWeapon = false;
  30.         input = Vector2.zero;
  31.        
  32.         currentVelocity = Vector3.zero;
  33.         gravityVelocity.y = 0;
  34.  
  35.         velocity = character.playerVelocity;
  36.         playerSpeed = character.playerSpeed;
  37.         grounded = character.controller.isGrounded;
  38.         gravityValue = character.gravityValue;    
  39.     }
  40.  
  41.     public override void HandleInput()
  42.     {
  43.         base.HandleInput();
  44.  
  45.         if (jumpAction.triggered)
  46.         {
  47.             jump = true;
  48.         }
  49.         if (crouchAction.triggered)
  50.         {
  51.             crouch = true;
  52.         }
  53.         if (sprintAction.triggered)
  54.         {
  55.             sprint = true;
  56.         }
  57.  
  58.         if (drawWeaponAction.triggered)
  59.         {
  60.             drawWeapon = true;
  61.         }
  62.  
  63.         input = moveAction.ReadValue<Vector2>();
  64.         velocity = new Vector3(input.x, 0, input.y);
  65.  
  66.         velocity = velocity.x * character.cameraTransform.right.normalized + velocity.z * character.cameraTransform.forward.normalized;
  67.         velocity.y = 0f;
  68.      
  69.     }
  70.  
  71.     public override void LogicUpdate()
  72.     {
  73.         base.LogicUpdate();
  74.  
  75.         character.animator.SetFloat("speed", input.magnitude, character.speedDampTime, Time.deltaTime);
  76.  
  77.         if (sprint)
  78.         {
  79.             stateMachine.ChangeState(character.sprinting);
  80.         }    
  81.         if (jump)
  82.         {
  83.             stateMachine.ChangeState(character.jumping);
  84.         }
  85.         if (crouch)
  86.         {
  87.             stateMachine.ChangeState(character.crouching);
  88.         }
  89.         if (drawWeapon)
  90.         {
  91.             stateMachine.ChangeState(character.combatting);
  92.             character.animator.SetTrigger("drawWeapon");
  93.         }
  94.     }
  95.  
  96.     public override void PhysicsUpdate()
  97.     {
  98.         base.PhysicsUpdate();
  99.  
  100.         gravityVelocity.y += gravityValue * Time.deltaTime;
  101.         grounded = character.controller.isGrounded;
  102.  
  103.         if (grounded && gravityVelocity.y < 0)
  104.         {
  105.             gravityVelocity.y = 0f;
  106.         }
  107.        
  108.         currentVelocity = Vector3.SmoothDamp(currentVelocity, velocity,ref cVelocity, character.velocityDampTime);
  109.         character.controller.Move(currentVelocity * Time.deltaTime * playerSpeed + gravityVelocity * Time.deltaTime);
  110.  
  111.         if (velocity.sqrMagnitude>0)
  112.         {
  113.             character.transform.rotation = Quaternion.Slerp(character.transform.rotation, Quaternion.LookRotation(velocity),character.rotationDampTime);
  114.         }
  115.        
  116.     }
  117.  
  118.     public override void Exit()
  119.     {
  120.         base.Exit();
  121.  
  122.         gravityVelocity.y = 0f;
  123.         character.playerVelocity = new Vector3(input.x, 0, input.y);
  124.  
  125.         if (velocity.sqrMagnitude > 0)
  126.         {
  127.             character.transform.rotation = Quaternion.LookRotation(velocity);
  128.         }
  129.     }
  130.  
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment