Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / 2D Customizable Characters / Common / Scripts / SimpleCharacterController.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(Rigidbody2D))]
  5. public class SimpleCharacterController : MonoBehaviour
  6. {
  7. public float moveSpeed = 70;
  8. public float m_MovementSmoothing = 0.1f;
  9. public bool normalizedMovement = true;
  10. public GameObject upObject;
  11. public GameObject leftObject;
  12. public GameObject rightObject;
  13. public GameObject downObject;
  14. Rigidbody2D rb;
  15. Animator currentAnimator;
  16. enum Direction { Up, Right, Down, Left };
  17. enum Expression { Neutral, Angry, Smile, Surprised };
  18. Direction currentDirection;
  19. Direction previousDirection;
  20. float angle = 180;
  21. float speed;
  22. Vector2 axisVector = Vector2.zero;
  23. Vector3 currentVelocity = Vector3.zero;
  24. Animator downAnimator;
  25. Animator upAnimator;
  26. Animator rightAnimator;
  27. Animator leftAnimator;
  28. void Start()
  29. {
  30. rb = GetComponent<Rigidbody2D>();
  31. upObject.SetActive(false);
  32. leftObject.SetActive(false);
  33. rightObject.SetActive(false);
  34. downObject.SetActive(true);
  35. downAnimator = downObject.GetComponent<Animator>();
  36. upAnimator = upObject.GetComponent<Animator>();
  37. rightAnimator = rightObject.GetComponent<Animator>();
  38. leftAnimator = leftObject.GetComponent<Animator>();
  39. currentAnimator = downAnimator;
  40. }
  41. void Update()
  42. {
  43. // get speed from the rigid body to be used for animator parameter Speed
  44. speed = rb.velocity.magnitude;
  45. // Get input axises
  46. axisVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  47. //normalize it for good topdown diagonal movement
  48. if (normalizedMovement == true)
  49. {
  50. axisVector.Normalize();
  51. }
  52. // Find out which direction to face and do what is appropiate
  53. //
  54. // Only update angle of direction if input axises are pressed
  55. if (!(axisVector.x == 0 && axisVector.y == 0))
  56. {
  57. // Find out what direction angle based on input axises
  58. angle = Mathf.Atan2(axisVector.x, axisVector.y) * Mathf.Rad2Deg;
  59. // Round out to prevent jittery direction changes.
  60. angle = Mathf.RoundToInt(angle);
  61. }
  62. if (angle > -45 && angle < 45) // UP
  63. {
  64. currentDirection = Direction.Up;
  65. }
  66. else if (angle < -135 || angle > 135) // DOWN
  67. {
  68. currentDirection = Direction.Down;
  69. }
  70. else if (angle >= 45 && angle <= 135) // RIGHT
  71. {
  72. currentDirection = Direction.Right;
  73. }
  74. else if (angle <= -45 && angle >= -135) // LEFT
  75. {
  76. currentDirection = Direction.Left;
  77. }
  78. // Did direction change?
  79. if (previousDirection != currentDirection)
  80. {
  81. if (currentDirection == Direction.Up)
  82. {
  83. // Activate appropiate game object
  84. upObject.SetActive(true);
  85. rightObject.SetActive(false);
  86. leftObject.SetActive(false);
  87. downObject.SetActive(false);
  88. currentAnimator = upAnimator;
  89. }
  90. else if (currentDirection == Direction.Down)
  91. {
  92. // Activate appropiate game object
  93. upObject.SetActive(false);
  94. rightObject.SetActive(false);
  95. leftObject.SetActive(false);
  96. downObject.SetActive(true);
  97. currentAnimator = downAnimator;
  98. }
  99. else if (currentDirection == Direction.Right)
  100. {
  101. // Activate appropiate game object
  102. upObject.SetActive(false);
  103. rightObject.SetActive(true);
  104. leftObject.SetActive(false);
  105. downObject.SetActive(false);
  106. currentAnimator = rightAnimator;
  107. }
  108. else if (currentDirection == Direction.Left)
  109. {
  110. // Activate appropiate game object
  111. upObject.SetActive(false);
  112. rightObject.SetActive(false);
  113. leftObject.SetActive(true);
  114. downObject.SetActive(false);
  115. currentAnimator = leftAnimator;
  116. }
  117. }
  118. // Set speed parameter to the animator
  119. currentAnimator.SetFloat("Speed", speed);
  120. // Set current direction as previous
  121. previousDirection = currentDirection;
  122. // Check keys for actions and use appropiate function
  123. //
  124. if (Input.GetKey(KeyCode.Space)) // SWING ATTACK
  125. {
  126. PlayAnimation("Swing");
  127. }
  128. else if (Input.GetKey(KeyCode.C)) // THRUST ATTACK
  129. {
  130. PlayAnimation("Thrust");
  131. }
  132. else if (Input.GetKey(KeyCode.X)) // BOW ATTACK
  133. {
  134. PlayAnimation("Bow");
  135. }
  136. else if (Input.GetKey(KeyCode.V)) // SET NEUTRAL FACE
  137. {
  138. SetExpression(Expression.Neutral);
  139. }
  140. else if (Input.GetKey(KeyCode.B)) // SET ANGRY FACE
  141. {
  142. SetExpression(Expression.Angry);
  143. }
  144. }
  145. void FixedUpdate()
  146. {
  147. // Move our character
  148. Move();
  149. }
  150. void PlayAnimation(string animationName)
  151. {
  152. // Play given animation in the current directions animator
  153. currentAnimator.Play(animationName, 0);
  154. }
  155. void SetExpression(Expression expressionToSet)
  156. {
  157. // convert enum to int for the animator paremeter.
  158. int expressionNumber = (int)expressionToSet;
  159. // If the current direction is not up change expression (Up direction doesn't show any expressions)
  160. if (!(currentDirection == Direction.Up)) // UP
  161. {
  162. currentAnimator.SetInteger("Expression", expressionNumber);
  163. }
  164. }
  165. void Move()
  166. {
  167. // Set target velocity to smooth towards
  168. Vector2 targetVelocity = new Vector2(axisVector.x * moveSpeed * 10f, axisVector.y * moveSpeed * 10) * Time.fixedDeltaTime;
  169. // Smoothing out the movement
  170. rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref currentVelocity, m_MovementSmoothing);
  171. }
  172. }