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.

146 lines
4.7 KiB

  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. [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
  7. [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
  8. [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
  9. [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
  10. [SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
  11. [SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
  12. [SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching
  13. const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
  14. private bool m_Grounded; // Whether or not the player is grounded.
  15. const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
  16. private Rigidbody2D m_Rigidbody2D;
  17. private bool m_FacingRight = true; // For determining which way the player is currently facing.
  18. private Vector3 m_Velocity = Vector3.zero;
  19. [Header("Events")]
  20. [Space]
  21. public UnityEvent OnLandEvent;
  22. [System.Serializable]
  23. public class BoolEvent : UnityEvent<bool> { }
  24. public BoolEvent OnCrouchEvent;
  25. private bool m_wasCrouching = false;
  26. private void Awake()
  27. {
  28. m_Rigidbody2D = GetComponent<Rigidbody2D>();
  29. if (OnLandEvent == null)
  30. OnLandEvent = new UnityEvent();
  31. if (OnCrouchEvent == null)
  32. OnCrouchEvent = new BoolEvent();
  33. }
  34. private void FixedUpdate()
  35. {
  36. bool wasGrounded = m_Grounded;
  37. m_Grounded = false;
  38. // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
  39. // This can be done using layers instead but Sample Assets will not overwrite your project settings.
  40. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
  41. for (int i = 0; i < colliders.Length; i++)
  42. {
  43. if (colliders[i].gameObject != gameObject)
  44. {
  45. m_Grounded = true;
  46. if (!wasGrounded)
  47. OnLandEvent.Invoke();
  48. }
  49. }
  50. }
  51. public void Move(float move, bool crouch, bool jump)
  52. {
  53. // If crouching, check to see if the character can stand up
  54. if (!crouch)
  55. {
  56. // If the character has a ceiling preventing them from standing up, keep them crouching
  57. if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
  58. {
  59. crouch = true;
  60. }
  61. }
  62. //only control the player if grounded or airControl is turned on
  63. if (m_Grounded || m_AirControl)
  64. {
  65. // If crouching
  66. if (crouch)
  67. {
  68. if (!m_wasCrouching)
  69. {
  70. m_wasCrouching = true;
  71. OnCrouchEvent.Invoke(true);
  72. }
  73. // Reduce the speed by the crouchSpeed multiplier
  74. move *= m_CrouchSpeed;
  75. // Disable one of the colliders when crouching
  76. if (m_CrouchDisableCollider != null)
  77. m_CrouchDisableCollider.enabled = false;
  78. }
  79. else
  80. {
  81. // Enable the collider when not crouching
  82. if (m_CrouchDisableCollider != null)
  83. m_CrouchDisableCollider.enabled = true;
  84. if (m_wasCrouching)
  85. {
  86. m_wasCrouching = false;
  87. OnCrouchEvent.Invoke(false);
  88. }
  89. }
  90. // Move the character by finding the target velocity
  91. Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
  92. // And then smoothing it out and applying it to the character
  93. m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
  94. // If the input is moving the player right and the player is facing left...
  95. if (move > 0 && !m_FacingRight)
  96. {
  97. // ... flip the player.
  98. Flip();
  99. }
  100. // Otherwise if the input is moving the player left and the player is facing right...
  101. else if (move < 0 && m_FacingRight)
  102. {
  103. // ... flip the player.
  104. Flip();
  105. }
  106. }
  107. // If the player should jump...
  108. if (m_Grounded && jump)
  109. {
  110. // Add a vertical force to the player.
  111. m_Grounded = false;
  112. m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
  113. }
  114. }
  115. private void Flip()
  116. {
  117. // Switch the way the player is labelled as facing.
  118. m_FacingRight = !m_FacingRight;
  119. // Multiply the player's x local scale by -1.
  120. Vector3 theScale = transform.localScale;
  121. theScale.x *= -1;
  122. transform.localScale = theScale;
  123. }
  124. }