Advertisement
Guest User

Untitled

a guest
Sep 13th, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class CharacterController2D : MonoBehaviour
  7. {
  8. public float WaitSeconds;
  9. public float LerpPower;
  10. [Range(0, 5)] [SerializeField] private float InsideLerpX = 1;
  11. [SerializeField] private float WallJumpHorizontalForce = 2.0f;
  12. [SerializeField] private float WallJumpVerticalForce = 300;
  13. [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
  14. [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
  15. [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement
  16. [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
  17. public bool WallJumpingMovement = false;
  18. [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
  19. [SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
  20. [SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings
  21. [SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching
  22. public Animator animator;
  23.  
  24. public Movement movescript; //Added by me
  25.  
  26. const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
  27. public bool m_Grounded; // Whether or not the player is grounded.
  28. public bool wasGrounded; //MINE
  29. const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
  30. private Rigidbody2D m_Rigidbody2D;
  31. public bool m_FacingRight = true; // For determining which way the player is currently facing.
  32. private Vector3 m_Velocity = Vector3.zero;
  33.  
  34. [Header("Events")]
  35. [Space]
  36.  
  37. public UnityEvent OnLandEvent;
  38.  
  39. [System.Serializable]
  40. public class BoolEvent : UnityEvent<bool> { }
  41.  
  42. public BoolEvent OnCrouchEvent;
  43. private bool m_wasCrouching = false;
  44.  
  45. private void Awake()
  46. {
  47. m_Rigidbody2D = GetComponent<Rigidbody2D>();
  48.  
  49. if (OnLandEvent == null)
  50. OnLandEvent = new UnityEvent();
  51.  
  52. if (OnCrouchEvent == null)
  53. OnCrouchEvent = new BoolEvent();
  54. }
  55.  
  56. private void FixedUpdate()
  57. {
  58. wasGrounded = m_Grounded;
  59. m_Grounded = false;
  60.  
  61. // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
  62. // This can be done using layers instead but Sample Assets will not overwrite your project settings.
  63. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
  64. for (int i = 0; i < colliders.Length; i++)
  65. {
  66. if (colliders[i].gameObject != gameObject)
  67. {
  68. m_Grounded = true;
  69. movescript.CoyoteWallJump = false;
  70. if (!wasGrounded)
  71. OnLandEvent.Invoke();
  72. }
  73. }
  74. }
  75.  
  76.  
  77. public void Move(float move, bool crouch, bool jump)
  78. {
  79. // If crouching, check to see if the character can stand up
  80. if (!crouch)
  81. {
  82. // If the character has a ceiling preventing them from standing up, keep them crouching
  83. if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
  84. {
  85. crouch = true;
  86. }
  87. }
  88.  
  89. //only control the player if grounded or airControl is turned on
  90. if (m_Grounded || m_AirControl)
  91. {
  92.  
  93. // If crouching
  94. if (crouch)
  95. {
  96. if (!m_wasCrouching)
  97. {
  98. m_wasCrouching = true;
  99. OnCrouchEvent.Invoke(true);
  100. }
  101.  
  102. // Reduce the speed by the crouchSpeed multiplier
  103. move *= m_CrouchSpeed;
  104.  
  105. // Disable one of the colliders when crouching
  106. if (m_CrouchDisableCollider != null)
  107. m_CrouchDisableCollider.enabled = false;
  108. }
  109. else
  110. {
  111. // Enable the collider when not crouching
  112. if (m_CrouchDisableCollider != null)
  113. m_CrouchDisableCollider.enabled = true;
  114.  
  115. if (m_wasCrouching)
  116. {
  117. m_wasCrouching = false;
  118. OnCrouchEvent.Invoke(false);
  119. }
  120. }
  121.  
  122. // Move the character by finding the target velocity
  123. Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
  124. // And then smoothing it out and applying it to the character
  125. if (!WallJumpingMovement) { m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing); }
  126. else if (WallJumpingMovement)// <-- <-- notice me senpai <-- <--
  127. {
  128. m_Rigidbody2D.velocity = Vector2.Lerp(m_Rigidbody2D.velocity, (new Vector2(move * InsideLerpX * 10f, m_Rigidbody2D.velocity.y)), LerpPower * Time.deltaTime); //THIS IS FOR WALL JUMPING
  129. }
  130.  
  131. // If the input is moving the player right and the player is facing left...
  132. if (move > 0 && !m_FacingRight && !WallJumpingMovement)
  133. {
  134. // ... flip the player.
  135. Flip();
  136. }
  137. // Otherwise if the input is moving the player left and the player is facing right...
  138. else if (move < 0 && m_FacingRight && !WallJumpingMovement)
  139. {
  140. // ... flip the player.
  141. Flip();
  142. }
  143. }
  144. // If the player should jump...
  145. if ((m_Grounded || movescript.CurrentlyWallSliding || movescript.CoyoteWallJump) && jump)
  146. {//Debug.Log("JUMP STATEMENT. CoyoteJump: " + movescript.CoyoteWallJump + " WallSliding: " + animator.GetBool("isWallSliding") + " Input: " + Input.GetAxisRaw("Horizontal"));
  147. if (movescript.CurrentlyWallSliding && !movescript.BackWallHit)
  148. {
  149. animator.Play("Player_WallJump");
  150. StartCoroutine(WallJump(movescript.ScaleAtJump));
  151. }
  152. else if ((movescript.BackWallHit || movescript.FrontCircleCast) && movescript.CoyoteWallJump)
  153. {
  154. animator.Play("Player_WallJump");
  155. if (movescript.BackWallHit) { StartCoroutine(WallJump(-movescript.ScaleAtJump)); } else { StartCoroutine(WallJump(movescript.ScaleAtJump)); }
  156. movescript.CoyoteWallJump = false;
  157. } else if (m_Grounded) { m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); }
  158. m_Grounded = false;
  159. }
  160. }
  161.  
  162. //WALL JUMPING CO-ROUTINE
  163. IEnumerator WallJump(float InputDirection)
  164. {
  165. //Debug.Log("InputDirection (inverted at AddForce): " + InputDirection + " Coyote: " + whichJump);
  166. WallJumpingMovement = true;
  167. m_Rigidbody2D.velocity = new Vector2(0f, 0f);
  168. m_Rigidbody2D.AddForce(new Vector2(WallJumpHorizontalForce * -InputDirection, 0), ForceMode2D.Impulse);
  169. m_Rigidbody2D.AddForce(new Vector2(0f, WallJumpVerticalForce));
  170. if (m_Rigidbody2D.velocity.x > 0 && !m_FacingRight) { Flip(); } else if (m_Rigidbody2D.velocity.x < 0 && m_FacingRight) { Flip(); }
  171. yield return new WaitForSeconds(WaitSeconds);
  172. WallJumpingMovement = false;
  173. }
  174.  
  175. private void Flip()
  176. {
  177. // Switch the way the player is labelled as facing.
  178. m_FacingRight = !m_FacingRight;
  179.  
  180. // Multiply the player's x local scale by -1.
  181. Vector3 theScale = transform.localScale;
  182. theScale.x *= -1;
  183. transform.localScale = theScale;
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement