JojikYT

CrouchingState

Dec 18th, 2021 (edited)
2,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class CrouchingState : State
  4. {
  5.     float playerSpeed;
  6.     bool belowCeiling;
  7.     bool crouchHeld;
  8.  
  9.     bool grounded;
  10.     float gravityValue;
  11.     Vector3 currentVelocity;
  12.  
  13.  
  14.     public CrouchingState(Character _character, StateMachine _stateMachine):base(_character, _stateMachine)
  15.     {
  16.         character = _character;
  17.         stateMachine = _stateMachine;
  18.     }
  19.  
  20.     public override void Enter()
  21.     {
  22.         base.Enter();
  23.  
  24.         character.animator.SetTrigger("crouch");  
  25.         belowCeiling = false;
  26.         crouchHeld = false;
  27.         gravityVelocity.y = 0;
  28.  
  29.         playerSpeed = character.crouchSpeed;
  30.         character.controller.height = character.crouchColliderHeight;
  31.         character.controller.center = new Vector3(0f, character.crouchColliderHeight / 2f, 0f);
  32.         grounded = character.controller.isGrounded;
  33.         gravityValue = character.gravityValue;
  34.  
  35.        
  36.     }
  37.  
  38.     public override void Exit()
  39.     {
  40.         base.Exit();
  41.         character.controller.height = character.normalColliderHeight;
  42.         character.controller.center = new Vector3(0f, character.normalColliderHeight / 2f, 0f);
  43.         gravityVelocity.y = 0f;
  44.         character.playerVelocity = new Vector3(input.x, 0, input.y);
  45.         character.animator.SetTrigger("move");
  46.     }
  47.  
  48.     public override void HandleInput()
  49.     {
  50.         base.HandleInput();
  51.         if (crouchAction.triggered && !belowCeiling)
  52.         {
  53.             crouchHeld = true;
  54.         }
  55.         input = moveAction.ReadValue<Vector2>();
  56.         velocity = new Vector3(input.x, 0, input.y);
  57.  
  58.         velocity = velocity.x * character.cameraTransform.right.normalized + velocity.z * character.cameraTransform.forward.normalized;
  59.         velocity.y = 0f;
  60.     }
  61.  
  62.     public override void LogicUpdate()
  63.     {
  64.         base.LogicUpdate();
  65.         character.animator.SetFloat("speed", input.magnitude, character.speedDampTime, Time.deltaTime);
  66.  
  67.         if (crouchHeld)
  68.         {
  69.             stateMachine.ChangeState(character.standing);
  70.         }
  71.     }
  72.  
  73.     public override void PhysicsUpdate()
  74.     {
  75.         base.PhysicsUpdate();
  76.         belowCeiling = CheckCollisionOverlap(character.transform.position + Vector3.up * character.normalColliderHeight);
  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.Lerp(currentVelocity, velocity, character.velocityDampTime);
  84.  
  85.         character.controller.Move(currentVelocity * Time.deltaTime * playerSpeed + gravityVelocity * Time.deltaTime);
  86.  
  87.         if (velocity.magnitude > 0)
  88.         {
  89.             character.transform.rotation = Quaternion.Slerp(character.transform.rotation, Quaternion.LookRotation(velocity), character.rotationDampTime);
  90.         }
  91.     }
  92.  
  93.     public bool CheckCollisionOverlap(Vector3 targetPositon)
  94.     {
  95.         int layerMask = 1 << 8;
  96.         layerMask = ~layerMask;
  97.         RaycastHit hit;
  98.  
  99.         Vector3 direction = targetPositon - character.transform.position;
  100.         if (Physics.Raycast(character.transform.position, direction, out hit, character.normalColliderHeight, layerMask))
  101.         {
  102.             Debug.DrawRay(character.transform.position, direction * hit.distance, Color.yellow);
  103.             return true;
  104.         }
  105.         else
  106.         {
  107.             Debug.DrawRay(character.transform.position, direction * character.normalColliderHeight, Color.white);
  108.             return false;
  109.         }      
  110.     }
  111.  
  112.  
  113. }
  114.  
Add Comment
Please, Sign In to add comment