Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. public class CharacterController2D : MonoBehaviour
  4. {
  5. [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
  6. [SerializeField] private float m_WallJumpMultiplier = 3f; // Amount of force added when the player jumps off a wall.
  7. [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
  8. [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
  9. [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
  10. [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
  11. [SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
  12. [SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
  13. [SerializeField] private Transform m_WallCheck;
  14. [SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching
  15. [SerializeField] private float m_wallFriction = 0.1f;
  16. const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
  17. private bool m_Grounded; // Whether or not the player is grounded.
  18. private bool m_TouchingWall;
  19. const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
  20. private Rigidbody2D m_Rigidbody2D;
  21. private bool m_FacingRight = true; // For determining which way the player is currently facing.
  22. private Vector3 m_Velocity = Vector3.zero;
  23. private enum Direction { NA=0, L=-1, R=1 };
  24. private Direction m_LastWallJumpDirection = Direction.NA;
  25. private int m_forward => (int) Mathf.Sign(transform.localScale.x);
  26. [Header("Events")]
  27. [Space]
  28. public UnityEvent OnJumpEvent;
  29. public UnityEvent OnLandEvent;
  30. [System.Serializable]
  31. public class BoolEvent : UnityEvent<bool> { }
  32. public BoolEvent OnCrouchEvent;
  33. private bool m_wasCrouching = false;
  34. private bool m_isJumpDown;
  35. private bool m_lastJump;
  36. private float m_lastWallJumpTime = 0;
  37. private float m_lastMove;
  38. private bool m_invertX;
  39. private void Awake()
  40. {
  41. m_Rigidbody2D = GetComponent<Rigidbody2D>();
  42. if (OnLandEvent == null)
  43. OnLandEvent = new UnityEvent();
  44. if (OnCrouchEvent == null)
  45. OnCrouchEvent = new BoolEvent();
  46. }
  47. private void FixedUpdate()
  48. {
  49. CheckGrounded();
  50. CheckTouchingWall();
  51. }
  52. private void CheckGrounded()
  53. {
  54. bool wasGrounded = m_Grounded;
  55. m_Grounded = false;
  56. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
  57. for (int i = 0; i < colliders.Length; i++)
  58. {
  59. if (colliders[i].gameObject != gameObject)
  60. {
  61. m_Grounded = true;
  62. if (!wasGrounded)
  63. OnLandEvent.Invoke();
  64. }
  65. }
  66. }
  67. private void CheckTouchingWall()
  68. {
  69. m_TouchingWall = false;
  70. if(!m_Grounded)
  71. {
  72. if(Physics2D.OverlapCircle(m_WallCheck.position, 0.2f, m_WhatIsGround))
  73. {
  74. m_TouchingWall = true;
  75. }
  76. }
  77. }
  78. public void Move(float move, bool crouch, bool jump)
  79. {
  80. // If crouching, check to see if the character can stand up
  81. if (!crouch)
  82. {
  83. // If the character has a ceiling preventing them from standing up, keep them crouching
  84. {
  85. crouch = true;
  86. }
  87. }
  88. m_isJumpDown = !m_lastJump && jump;
  89. m_lastJump = jump;
  90. bool hasMoveChanged = System.Math.Sign(m_lastMove) != System.Math.Sign(move);
  91. m_lastMove = move;
  92. Debug.Log($"move Changed {hasMoveChanged}");
  93. //only control the player if grounded or airControl is turned on
  94. if (m_Grounded || m_AirControl)
  95. {
  96. // If crouching
  97. if (crouch)
  98. {
  99. if (!m_wasCrouching)
  100. {
  101. m_wasCrouching = true;
  102. OnCrouchEvent.Invoke(true);
  103. }
  104. // Reduce the speed by the crouchSpeed multiplier
  105. move *= m_CrouchSpeed;
  106. // Disable one of the colliders when crouching
  107. if (m_CrouchDisableCollider != null)
  108. m_CrouchDisableCollider.enabled = false;
  109. }
  110. else
  111. {
  112. // Enable the collider when not crouching
  113. if (m_CrouchDisableCollider != null)
  114. m_CrouchDisableCollider.enabled = true;
  115. if (m_wasCrouching)
  116. {
  117. m_wasCrouching = false;
  118. OnCrouchEvent.Invoke(false);
  119. }
  120. }
  121. // Move the character by finding the target velocity
  122. Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
  123. if (m_invertX)
  124. {
  125. Debug.Log("Inverting Direction");
  126. targetVelocity *= new Vector2(-1, 1);
  127. }
  128. if (hasMoveChanged || m_Grounded)
  129. m_invertX = false;
  130. // And then smoothing it out and applying it to the character
  131. m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
  132. // If the input is moving the player right and the player is facing left...
  133. if (move > 0 && !m_FacingRight)
  134. {
  135. // ... flip the player.
  136. Flip();
  137. }
  138. // Otherwise if the input is moving the player left and the player is facing right...
  139. else if (move < 0 && m_FacingRight)
  140. {
  141. // ... flip the player.
  142. Flip();
  143. }
  144. }
  145. // If the player should jump...
  146. if (m_Grounded && m_isJumpDown)
  147. {
  148. // Add a vertical force to the player.
  149. m_Grounded = false;
  150. m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
  151. OnJumpEvent.Invoke();
  152. m_LastWallJumpDirection = Direction.NA;
  153. Debug.Log("Regular Jump");
  154. }
  155. //else if (m_TouchingWall && jump)
  156. //{
  157. // if (m_FacingRight && m_LastWallJumpDirection == Direction.L)
  158. // {
  159. // m_Rigidbody2D.AddForce(new Vector2(m_JumpForce * m_WallJumpMultiplier * (m_FacingRight ? 1f : -1f), m_JumpForce * m_WallJumpMultiplier));
  160. // OnJumpEvent.Invoke();
  161. //
  162. // m_LastWallJumpDirection = Direction.R;
  163. // }
  164. // else if (!m_FacingRight && m_LastWallJumpDirection == Direction.R)
  165. // {
  166. // m_Rigidbody2D.AddForce(new Vector2(m_JumpForce * m_WallJumpMultiplier * (m_FacingRight ? 1f : -1f), m_JumpForce * m_WallJumpMultiplier));
  167. // OnJumpEvent.Invoke();
  168. //
  169. // m_LastWallJumpDirection = Direction.L;
  170. // }
  171. //
  172. //}
  173. else if(m_TouchingWall && m_isJumpDown && (int) m_LastWallJumpDirection != m_forward)
  174. {
  175. m_Rigidbody2D.AddForce(new Vector2(m_WallJumpMultiplier * -m_forward, m_WallJumpMultiplier),ForceMode2D.Impulse);
  176. OnJumpEvent.Invoke();
  177. m_LastWallJumpDirection = (m_forward == -1) ? Direction.L : Direction.R;
  178. Debug.Log("Wall Jump");
  179. m_lastWallJumpTime = Time.time;
  180. m_invertX = true;
  181. }
  182. if (m_TouchingWall && move != 0 && !m_Grounded && m_Rigidbody2D.velocity.y < 0)
  183. {
  184. Vector2 velocity = m_Rigidbody2D.velocity;
  185. velocity.y = Mathf.Lerp(velocity.y, 0, m_wallFriction);
  186. m_Rigidbody2D.velocity = velocity;
  187. }
  188. }
  189. private void Flip()
  190. {
  191. // Switch the way the player is labelled as facing.
  192. m_FacingRight = !m_FacingRight;
  193. // Multiply the player's x local scale by -1.
  194. Vector3 theScale = transform.localScale;
  195. theScale.x *= -1;
  196. transform.localScale = theScale;
  197. }
  198. private void OnDrawGizmosSelected()
  199. {
  200. Gizmos.color = m_TouchingWall ? Color.yellow : Color.green;
  201. Gizmos.DrawWireSphere(m_WallCheck.position, 0.2f);
  202. Gizmos.color = m_Grounded ? Color.yellow : Color.green;
  203. Gizmos.DrawWireSphere(m_GroundCheck.position, k_GroundedRadius);
  204. }
  205. }