JojikYT

CombatState

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