Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. public class CharacterController2D : MonoBehaviour
  5. {
  6. public Transform mire;
  7. [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
  8. [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
  9. [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
  10. [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
  11. [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
  12. [SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
  13. [SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
  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. const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
  18. private Rigidbody2D m_Rigidbody2D;
  19. private bool m_FacingRight = true; // For determining which way the player is currently facing.
  20. private Vector3 m_Velocity = Vector3.zero;
  21.  
  22. [Header("Events")]
  23. [Space]
  24.  
  25. public UnityEvent OnLandEvent;
  26.  
  27. [System.Serializable]
  28. public class BoolEvent : UnityEvent<bool> { }
  29.  
  30. public BoolEvent OnCrouchEvent;
  31. private bool m_wasCrouching = false;
  32.  
  33. private void Awake()
  34. {
  35. m_Rigidbody2D = GetComponent<Rigidbody2D>();
  36.  
  37. if (OnLandEvent == null)
  38. OnLandEvent = new UnityEvent();
  39.  
  40. if (OnCrouchEvent == null)
  41. OnCrouchEvent = new BoolEvent();
  42. }
  43.  
  44. private void FixedUpdate()
  45. {
  46. bool wasGrounded = m_Grounded;
  47. m_Grounded = false;
  48.  
  49. // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
  50. // This can be done using layers instead but Sample Assets will not overwrite your project settings.
  51. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
  52. for (int i = 0; i < colliders.Length; i++)
  53. {
  54. if (colliders[i].gameObject != gameObject)
  55. {
  56. m_Grounded = true;
  57. if (!wasGrounded && m_Rigidbody2D.velocity.y <= 0)
  58. OnLandEvent.Invoke();
  59.  
  60. }
  61. }
  62. }
  63.  
  64.  
  65. public void Move(float move, bool crouch, bool jump)
  66. {
  67. // If crouching, check to see if the character can stand up
  68. if (!crouch)
  69. {
  70. // If the character has a ceiling preventing them from standing up, keep them crouching
  71. if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
  72. {
  73. crouch = true;
  74. }
  75. }
  76.  
  77. //only control the player if grounded or airControl is turned on
  78. if (m_Grounded || m_AirControl)
  79. {
  80.  
  81. // If crouching
  82. if (crouch)
  83. {
  84. if (!m_wasCrouching)
  85. {
  86. m_wasCrouching = true;
  87. OnCrouchEvent.Invoke(true);
  88. }
  89.  
  90. // Reduce the speed by the crouchSpeed multiplier
  91. move *= m_CrouchSpeed;
  92.  
  93. // Disable one of the colliders when crouching
  94. if (m_CrouchDisableCollider != null)
  95. m_CrouchDisableCollider.enabled = false;
  96. } else
  97. {
  98. // Enable the collider when not crouching
  99. if (m_CrouchDisableCollider != null)
  100. m_CrouchDisableCollider.enabled = true;
  101.  
  102. if (m_wasCrouching)
  103. {
  104. m_wasCrouching = false;
  105. OnCrouchEvent.Invoke(false);
  106. }
  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.  
  114. // If the input is moving the player right and the player is facing left...
  115. if (move > 0 && !m_FacingRight)
  116. {
  117. // ... flip the player.
  118. Flip();
  119. }
  120. // Otherwise if the input is moving the player left and the player is facing right...
  121. else if (move < 0 && m_FacingRight)
  122. {
  123. // ... flip the player.
  124. Flip();
  125. }
  126. }
  127. // If the player should jump...
  128. if ( m_Grounded && jump)
  129. {
  130.  
  131. // Add a vertical force to the player.
  132. m_Grounded = false;
  133. m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
  134.  
  135. }
  136. }
  137.  
  138.  
  139. private void Flip()
  140. {
  141. // Switch the way the player is labelled as facing.
  142. m_FacingRight = !m_FacingRight;
  143.  
  144. // Multiply the player's x local scale by -1.
  145. Vector3 theScale = transform.localScale;
  146. theScale.x *= -1;
  147. transform.localScale = theScale;
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement