JojikYT

AttackState

Mar 14th, 2022 (edited)
2,813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using UnityEngine;
  2. public class AttackState : State
  3. {
  4.     float timePassed;
  5.     float clipLength;
  6.     float clipSpeed;
  7.     bool attack;
  8.     public AttackState(Character _character, StateMachine _stateMachine) : base(_character, _stateMachine)
  9.     {
  10.         character = _character;
  11.         stateMachine = _stateMachine;
  12.     }
  13.  
  14.     public override void Enter()
  15.     {
  16.         base.Enter();
  17.  
  18.         attack = false;
  19.         character.animator.applyRootMotion = true;
  20.         timePassed = 0f;
  21.         character.animator.SetTrigger("attack");
  22.         character.animator.SetFloat("speed", 0f);
  23.     }
  24.  
  25.     public override void HandleInput()
  26.     {
  27.         base.HandleInput();
  28.  
  29.         if (attackAction.triggered)
  30.         {
  31.             attack = true;
  32.         }
  33.     }
  34.     public override void LogicUpdate()
  35.     {
  36.         base.LogicUpdate();
  37.  
  38.         timePassed += Time.deltaTime;
  39.         clipLength = character.animator.GetCurrentAnimatorClipInfo(1)[0].clip.length;
  40.         clipSpeed = character.animator.GetCurrentAnimatorStateInfo(1).speed;
  41.  
  42.         if (timePassed >= clipLength / clipSpeed && attack)
  43.         {
  44.             stateMachine.ChangeState(character.attacking);
  45.         }
  46.         if (timePassed >= clipLength / clipSpeed)
  47.         {
  48.             stateMachine.ChangeState(character.combatting);
  49.             character.animator.SetTrigger("move");
  50.         }
  51.  
  52.     }
  53.     public override void Exit()
  54.     {
  55.         base.Exit();
  56.         character.animator.applyRootMotion = false;
  57.     }
  58. }
  59.  
  60.  
Add Comment
Please, Sign In to add comment