GameKitCharacterBrain.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Animancer.Units;
  5. using UnityEngine;
  6. using static Animancer.Validate;
  7. namespace Animancer.Examples.AnimatorControllers.GameKit
  8. {
  9. /// <summary>A brain which controls the character using keyboard input.</summary>
  10. /// <remarks>This class serves the same purpose as <c>PlayerInput</c> from the 3D Game Kit.</remarks>
  11. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/animator-controllers/3d-game-kit">3D Game Kit</see></example>
  12. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.AnimatorControllers.GameKit/GameKitCharacterBrain
  13. ///
  14. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Game Kit - Game Kit Character Brain")]
  15. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(AnimatorControllers) + "." + nameof(GameKit) + "/" + nameof(GameKitCharacterBrain))]
  16. public sealed class GameKitCharacterBrain : MonoBehaviour
  17. {
  18. /************************************************************************************************************************/
  19. [SerializeField] private Character _Character;
  20. [SerializeField] private AirborneState _Jump;
  21. [SerializeField] private CharacterState _Attack;
  22. [SerializeField]
  23. [Seconds(Rule = Value.IsNotNegative)]
  24. private float _AttackInputTimeOut = 0.5f;
  25. private StateMachine<CharacterState>.InputBuffer _InputBuffer;
  26. /************************************************************************************************************************/
  27. private void Awake()
  28. {
  29. _InputBuffer = new StateMachine<CharacterState>.InputBuffer(_Character.StateMachine);
  30. }
  31. /************************************************************************************************************************/
  32. private void Update()
  33. {
  34. UpdateMovement();
  35. UpdateActions();
  36. }
  37. /************************************************************************************************************************/
  38. private void UpdateMovement()
  39. {
  40. var input = ExampleInput.WASD;
  41. if (input == default)
  42. {
  43. _Character.Parameters.MovementDirection = default;
  44. return;
  45. }
  46. // Get the camera's forward and right vectors and flatten them onto the XZ plane.
  47. var camera = Camera.main.transform;
  48. var forward = camera.forward;
  49. forward.y = 0;
  50. forward.Normalize();
  51. var right = camera.right;
  52. right.y = 0;
  53. right.Normalize();
  54. // Build the movement vector by multiplying the input by those axes.
  55. _Character.Parameters.MovementDirection =
  56. right * input.x +
  57. forward * input.y;
  58. }
  59. /************************************************************************************************************************/
  60. private void UpdateActions()
  61. {
  62. // Jump gets priority for better platforming.
  63. if (ExampleInput.SpaceDown)
  64. {
  65. _Jump.TryJump();
  66. }
  67. else if (ExampleInput.SpaceUp)
  68. {
  69. _Jump.CancelJump();
  70. }
  71. if (ExampleInput.LeftMouseDown)
  72. {
  73. _InputBuffer.Buffer(_Attack, _AttackInputTimeOut);
  74. }
  75. _InputBuffer.Update();
  76. }
  77. /************************************************************************************************************************/
  78. }
  79. }