Skip to content

Instantly share code, notes, and snippets.

@yektasarioglu
Created March 8, 2021 08:21
Show Gist options
  • Save yektasarioglu/505c00f905132ac7302f5c623aa4ada2 to your computer and use it in GitHub Desktop.
Save yektasarioglu/505c00f905132ac7302f5c623aa4ada2 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class CharacterStateMachine : StateMachineBehaviour
{
public static CharacterAnimationState characterAnimationState;
public static BaseCharacter baseCharacter;
public static void setBaseCharacter<T>(T character) where T : BaseCharacter
{
baseCharacter = character;
}
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Debug.Log("CharacterStateMachine - OnStateEnter() - " + baseCharacter.name);
characterAnimationState = getCharacterState(animator, true);
}
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Debug.Log("CharacterStateMachine - OnStateExit() - " + baseCharacter.name);
characterAnimationState = getCharacterState(animator, false);
}
private CharacterAnimationState getCharacterState(Animator animator, bool isStateEntered)
{
CharacterAnimationState characterAnimationState = CharacterAnimationState.NoAnimation;
if (isStateEntered)
{
if (baseCharacter.headerType != null && animator.GetCurrentAnimatorStateInfo(0).IsName(baseCharacter.headerType.Value))
{
characterAnimationState = CharacterAnimationState.HeadingStarted;
baseCharacter.OnHeadingStarted();
}
else if (animator.GetCurrentAnimatorStateInfo(0).IsName("Strafe Walking"))
{
characterAnimationState = CharacterAnimationState.StrafeWalkingStarted;
baseCharacter.OnStrafeWalkingStarted();
}
else if (animator.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
{
characterAnimationState = CharacterAnimationState.Idle;
}
}
else
{
if (animator.GetCurrentAnimatorStateInfo(0).IsName(baseCharacter.headerType.Value))
{
Debug.Log("Heading is finished !!");
characterAnimationState = CharacterAnimationState.HeadingEnded;
baseCharacter.OnHeadingFinished();
}
else if (animator.GetCurrentAnimatorStateInfo(0).IsName("Strafe Walking"))
{
characterAnimationState = CharacterAnimationState.StrafeWalkingEnded;
baseCharacter.OnStrafeWalkingFinished();
}
}
return characterAnimationState;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment