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.

191 lines
6.1 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
  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. const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
  16. private bool m_Grounded; // Whether or not the player is grounded.
  17. private bool m_TouchingWall;
  18. const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
  19. private Rigidbody2D m_Rigidbody2D;
  20. private bool m_FacingRight = true; // For determining which way the player is currently facing.
  21. private Vector3 m_Velocity = Vector3.zero;
  22. private enum Direction { NA=0, L, R };
  23. private Direction m_LastWallJumpDirection = Direction.NA;
  24. [Header("Events")]
  25. [Space]
  26. public UnityEvent OnJumpEvent;
  27. public UnityEvent OnLandEvent;
  28. [System.Serializable]
  29. public class BoolEvent : UnityEvent<bool> { }
  30. public BoolEvent OnCrouchEvent;
  31. private bool m_wasCrouching = false;
  32. private void Awake()
  33. {
  34. m_Rigidbody2D = GetComponent<Rigidbody2D>();
  35. if (OnLandEvent == null)
  36. OnLandEvent = new UnityEvent();
  37. if (OnCrouchEvent == null)
  38. OnCrouchEvent = new BoolEvent();
  39. }
  40. private void FixedUpdate()
  41. {
  42. CheckGrounded();
  43. CheckTouchingWall();
  44. }
  45. private void CheckGrounded()
  46. {
  47. bool wasGrounded = m_Grounded;
  48. m_Grounded = false;
  49. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
  50. for (int i = 0; i < colliders.Length; i++)
  51. {
  52. if (colliders[i].gameObject != gameObject)
  53. {
  54. m_Grounded = true;
  55. if (!wasGrounded)
  56. OnLandEvent.Invoke();
  57. }
  58. }
  59. }
  60. private void CheckTouchingWall()
  61. {
  62. m_TouchingWall = false;
  63. if(!m_Grounded)
  64. {
  65. if(Physics2D.OverlapCircle(m_WallCheck.position, 0.2f, m_WhatIsGround))
  66. {
  67. m_TouchingWall = true;
  68. }
  69. }
  70. }
  71. public void Move(float move, bool crouch, bool jump)
  72. {
  73. // If crouching, check to see if the character can stand up
  74. if (!crouch)
  75. {
  76. // If the character has a ceiling preventing them from standing up, keep them crouching
  77. {
  78. crouch = true;
  79. }
  80. }
  81. //only control the player if grounded or airControl is turned on
  82. if (m_Grounded || m_AirControl)
  83. {
  84. // If crouching
  85. if (crouch)
  86. {
  87. if (!m_wasCrouching)
  88. {
  89. m_wasCrouching = true;
  90. OnCrouchEvent.Invoke(true);
  91. }
  92. // Reduce the speed by the crouchSpeed multiplier
  93. move *= m_CrouchSpeed;
  94. // Disable one of the colliders when crouching
  95. if (m_CrouchDisableCollider != null)
  96. m_CrouchDisableCollider.enabled = false;
  97. }
  98. else
  99. {
  100. // Enable the collider when not crouching
  101. if (m_CrouchDisableCollider != null)
  102. m_CrouchDisableCollider.enabled = true;
  103. if (m_wasCrouching)
  104. {
  105. m_wasCrouching = false;
  106. OnCrouchEvent.Invoke(false);
  107. }
  108. }
  109. // Move the character by finding the target velocity
  110. Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
  111. // And then smoothing it out and applying it to the character
  112. m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
  113. // If the input is moving the player right and the player is facing left...
  114. if (move > 0 && !m_FacingRight)
  115. {
  116. // ... flip the player.
  117. Flip();
  118. }
  119. // Otherwise if the input is moving the player left and the player is facing right...
  120. else if (move < 0 && m_FacingRight)
  121. {
  122. // ... flip the player.
  123. Flip();
  124. }
  125. }
  126. // If the player should jump...
  127. if (m_Grounded && jump)
  128. {
  129. // Add a vertical force to the player.
  130. m_Grounded = false;
  131. m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
  132. OnJumpEvent.Invoke();
  133. // Reset wall jump switch
  134. m_LastWallJumpDirection = move > 0 ? Direction.R : Direction.L;
  135. }
  136. else if (m_TouchingWall && jump)
  137. {
  138. if (m_FacingRight && m_LastWallJumpDirection == Direction.L)
  139. {
  140. m_Rigidbody2D.AddForce(new Vector2(m_JumpForce * m_WallJumpMultiplier * (m_FacingRight ? 1f : -1f), m_JumpForce * m_WallJumpMultiplier));
  141. OnJumpEvent.Invoke();
  142. m_LastWallJumpDirection = Direction.R;
  143. }
  144. else if (!m_FacingRight && m_LastWallJumpDirection == Direction.R)
  145. {
  146. m_Rigidbody2D.AddForce(new Vector2(m_JumpForce * m_WallJumpMultiplier * (m_FacingRight ? 1f : -1f), m_JumpForce * m_WallJumpMultiplier));
  147. OnJumpEvent.Invoke();
  148. m_LastWallJumpDirection = Direction.L;
  149. }
  150. }
  151. }
  152. private void Flip()
  153. {
  154. // Switch the way the player is labelled as facing.
  155. m_FacingRight = !m_FacingRight;
  156. // Multiply the player's x local scale by -1.
  157. Vector3 theScale = transform.localScale;
  158. theScale.x *= -1;
  159. transform.localScale = theScale;
  160. }
  161. }