JojikYT

SprintState

Dec 18th, 2021 (edited)
2,923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using UnityEngine;
  2. public class SprintState : State
  3. {
  4.     float gravityValue;
  5.     Vector3 currentVelocity;
  6.  
  7.     bool grounded;
  8.     bool sprint;
  9.     float playerSpeed;
  10.     bool sprintJump;
  11.     Vector3 cVelocity;
  12.     public SprintState(Character _character, StateMachine _stateMachine) : base(_character, _stateMachine)
  13.     {
  14.         character = _character;
  15.         stateMachine = _stateMachine;
  16.     }
  17.  
  18.     public override void Enter()
  19.     {
  20.         base.Enter();
  21.  
  22.         sprint = false;
  23.         sprintJump = false;
  24.         input = Vector2.zero;
  25.         velocity = Vector3.zero;
  26.         currentVelocity = Vector3.zero;
  27.         gravityVelocity.y = 0;
  28.  
  29.         playerSpeed = character.sprintSpeed;
  30.         grounded = character.controller.isGrounded;
  31.         gravityValue = character.gravityValue;        
  32.     }
  33.  
  34.     public override void HandleInput()
  35.     {
  36.         base.Enter();
  37.         input = moveAction.ReadValue<Vector2>();
  38.         velocity = new Vector3(input.x, 0, input.y);
  39.  
  40.         velocity = velocity.x * character.cameraTransform.right.normalized + velocity.z * character.cameraTransform.forward.normalized;
  41.         velocity.y = 0f;
  42.         if (sprintAction.triggered || input.sqrMagnitude == 0f)
  43.         {
  44.             sprint = false;
  45.         }
  46.         else
  47.         {
  48.             sprint = true;
  49.         }
  50.         if (jumpAction.triggered)
  51.         {
  52.             sprintJump = true;
  53.  
  54.         }
  55.  
  56.     }
  57.  
  58.     public override void LogicUpdate()
  59.     {
  60.         if (sprint)
  61.         {
  62.             character.animator.SetFloat("speed", input.magnitude + 0.5f, character.speedDampTime, Time.deltaTime);
  63.         }
  64.         else
  65.         {
  66.             stateMachine.ChangeState(character.standing);
  67.         }
  68.         if (sprintJump)
  69.         {
  70.             stateMachine.ChangeState(character.sprintjumping);
  71.         }
  72.     }
  73.  
  74.     public override void PhysicsUpdate()
  75.     {
  76.         base.PhysicsUpdate();
  77.         gravityVelocity.y += gravityValue * Time.deltaTime;
  78.         grounded = character.controller.isGrounded;
  79.         if (grounded && gravityVelocity.y < 0)
  80.         {
  81.             gravityVelocity.y = 0f;
  82.         }
  83.         currentVelocity = Vector3.SmoothDamp(currentVelocity, velocity, ref cVelocity, character.velocityDampTime);
  84.  
  85.         character.controller.Move(currentVelocity * Time.deltaTime * playerSpeed + gravityVelocity * Time.deltaTime);
  86.  
  87.  
  88.         if (velocity.sqrMagnitude > 0)
  89.         {
  90.             character.transform.rotation = Quaternion.Slerp(character.transform.rotation, Quaternion.LookRotation(velocity), character.rotationDampTime);
  91.         }
  92.     }
  93. }
  94.  
Add Comment
Please, Sign In to add comment