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.

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