CharacterState.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using Animancer.FSM;
  4. using UnityEngine;
  5. namespace Animancer.Examples.AnimatorControllers.GameKit
  6. {
  7. /// <summary>
  8. /// Base class for the various states a <see cref="Brains.Character"/> can be in and actions they can perform.
  9. /// </summary>
  10. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/animator-controllers/3d-game-kit">3D Game Kit</see></example>
  11. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.AnimatorControllers.GameKit/CharacterState
  12. ///
  13. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Game Kit - Character State")]
  14. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(AnimatorControllers) + "." + nameof(GameKit) + "/" + nameof(CharacterState))]
  15. public abstract class CharacterState : StateBehaviour, IOwnedState<CharacterState>
  16. {
  17. /************************************************************************************************************************/
  18. [System.Serializable]
  19. public class StateMachine : StateMachine<CharacterState>.WithDefault
  20. {
  21. /************************************************************************************************************************/
  22. [SerializeField]
  23. private CharacterState _Locomotion;
  24. public CharacterState Locomotion => _Locomotion;
  25. [SerializeField]
  26. private CharacterState _Airborne;
  27. public CharacterState Airborne => _Airborne;
  28. /************************************************************************************************************************/
  29. }
  30. /************************************************************************************************************************/
  31. [SerializeField]
  32. private Character _Character;
  33. public Character Character => _Character;
  34. /************************************************************************************************************************/
  35. #if UNITY_EDITOR
  36. protected override void OnValidate()
  37. {
  38. base.OnValidate();
  39. gameObject.GetComponentInParentOrChildren(ref _Character);
  40. }
  41. #endif
  42. /************************************************************************************************************************/
  43. public StateMachine<CharacterState> OwnerStateMachine => _Character.StateMachine;
  44. /************************************************************************************************************************/
  45. /// <summary>
  46. /// Jumping enters the <see cref="AirborneState"/>, but <see cref="CharacterController.isGrounded"/> doesn't
  47. /// become false until after the first update, so we want to make sure the <see cref="Character"/> won't stick
  48. /// to the ground during that update.
  49. /// </summary>
  50. public virtual bool StickToGround => true;
  51. /// <summary>
  52. /// Some states (such as <see cref="AirborneState"/>) will want to apply their own source of root motion, but
  53. /// most will just use the root motion from the animations.
  54. /// </summary>
  55. public virtual Vector3 RootMotion => _Character.Animancer.Animator.deltaPosition;
  56. /// <summary>
  57. /// Indicates whether the root motion applied each frame while this state is active should be constrained to
  58. /// only move in the specified <see cref="CharacterBrain.Movement"/>. Otherwise the root motion can
  59. /// move the <see cref="Character"/> in any direction. Default is true.
  60. /// </summary>
  61. public virtual bool FullMovementControl => true;
  62. /************************************************************************************************************************/
  63. }
  64. }