Advertisement
Guest User

State Machine

a guest
Nov 14th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. //Character Move Manager
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. namespace game_Input
  7.  
  8. {
  9. public enum TransitionParameter
  10. {
  11. Moving,
  12. }
  13. public class characterMoveManager : MonoBehaviour
  14. {
  15. public float speed;
  16. public Animator animator;
  17. }
  18. }
  19.  
  20. //Character State Base
  21. using System.Collections;
  22. using System.Collections.Generic;
  23. using UnityEngine;
  24.  
  25. namespace game_Input
  26. {
  27. public class characterStateBase : StateMachineBehaviour
  28. {
  29. private characterMoveManager characterControl;
  30. public characterMoveManager GetCharacterControl(Animator animator)
  31. {
  32. if (characterControl == null)
  33. {
  34. characterControl = animator.GetComponentInParent<characterMoveManager>();
  35. }
  36. return characterControl;
  37. }
  38. }
  39. }
  40.  
  41.  
  42. //Idle State
  43. using System.Collections;
  44. using System.Collections.Generic;
  45. using UnityEngine;
  46.  
  47. namespace game_Input
  48. {
  49. public class PlayerIdle : characterStateBase
  50. {
  51. // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
  52. override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  53. {
  54.  
  55. }
  56.  
  57. // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
  58. override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  59. {
  60. if (VirtualInputManager.Instance.moveRight || VirtualInputManager.Instance.moveLeft || VirtualInputManager.Instance.moveForward || VirtualInputManager.Instance.moveBackward)
  61. {
  62. animator.SetBool(TransitionParameter.Moving.ToString(), true);
  63. }
  64. }
  65.  
  66. // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
  67. override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  68. {
  69.  
  70. }
  71. }
  72. }
  73.  
  74. //Walk State
  75. using System.Collections;
  76. using System.Collections.Generic;
  77. using UnityEngine;
  78.  
  79. namespace game_Input
  80. {
  81. public class PlayerWalk : characterStateBase
  82. {
  83. // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
  84. override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  85. {
  86. }
  87.  
  88. // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
  89. override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  90. {
  91.  
  92. if (VirtualInputManager.Instance.moveRight && VirtualInputManager.Instance.moveLeft)
  93. {
  94. animator.SetBool(TransitionParameter.Moving.ToString(), false);
  95. return;
  96. }
  97.  
  98. if (VirtualInputManager.Instance.moveForward && VirtualInputManager.Instance.moveBackward)
  99. {
  100. animator.SetBool(TransitionParameter.Moving.ToString(), false);
  101. return;
  102. }
  103.  
  104. if (!VirtualInputManager.Instance.moveRight && !VirtualInputManager.Instance.moveLeft && !VirtualInputManager.Instance.moveForward && !VirtualInputManager.Instance.moveBackward)
  105. {
  106. animator.SetBool(TransitionParameter.Moving.ToString(), false);
  107. return;
  108. }
  109.  
  110. if (VirtualInputManager.Instance.moveForward)
  111. {
  112. GetCharacterControl(animator).transform.Translate(Vector3.forward * GetCharacterControl(animator).speed * Time.deltaTime);
  113. GetCharacterControl(animator).transform.rotation = Quaternion.Euler(0f, 0f, 0f);
  114. }
  115.  
  116. if (VirtualInputManager.Instance.moveBackward)
  117. {
  118. GetCharacterControl(animator).transform.Translate(Vector3.forward * GetCharacterControl(animator).speed * Time.deltaTime);
  119. GetCharacterControl(animator).transform.rotation = Quaternion.Euler(0f, 180f, 0f);
  120. }
  121.  
  122. if (VirtualInputManager.Instance.moveRight)
  123. {
  124. GetCharacterControl(animator).transform.Translate(Vector3.forward * GetCharacterControl(animator).speed * Time.deltaTime);
  125. GetCharacterControl(animator).transform.rotation = Quaternion.Euler(0f, 90f, 0f);
  126. }
  127.  
  128. if (VirtualInputManager.Instance.moveLeft)
  129. {
  130. GetCharacterControl(animator).transform.Translate(Vector3.forward * GetCharacterControl(animator).speed * Time.deltaTime);
  131. GetCharacterControl(animator).transform.rotation = Quaternion.Euler(0f, -90f, 0f);
  132. }
  133. }
  134.  
  135. // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
  136. override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  137. {
  138.  
  139. }
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement