jamieTheCoder

CharacterController2D

Sep 2nd, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. public class CharacterController2D : MonoBehaviour {
  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.     public int NumberOfJumps; // int describing number of jumps done
  14.     public int MaxJumps; // max number of jumps
  15.  
  16.     const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
  17.     public bool m_Grounded; // Whether or not the player is grounded.
  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.  
  23.     [Header ("Events")]
  24.     [Space]
  25.  
  26.     public UnityEvent OnLandEvent;
  27.  
  28.     [System.Serializable]
  29.     public class BoolEvent : UnityEvent<bool> { }
  30.  
  31.     public BoolEvent OnCrouchEvent;
  32.     private bool m_wasCrouching = false;
  33.  
  34.     private void Awake () {
  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.         bool wasGrounded = m_Grounded;
  46.         m_Grounded = false;
  47.  
  48.         // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
  49.         // This can be done using layers instead but Sample Assets will not overwrite your project settings.
  50.         Collider2D[] colliders = Physics2D.OverlapCircleAll (m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
  51.         for (int i = 0; i < colliders.Length; i++) {
  52.             if (colliders[i].gameObject != gameObject) {
  53.                 m_Grounded = true;
  54.                 NumberOfJumps = 0;
  55.                 if (!wasGrounded)
  56.                     OnLandEvent.Invoke ();
  57.             }
  58.         }
  59.     }
  60.  
  61.     public void Move (float move, bool crouch, bool jump) {
  62.         // If crouching, check to see if the character can stand up
  63.         if (!crouch) {
  64.             // If the character has a ceiling preventing them from standing up, keep them crouching
  65.             if (Physics2D.OverlapCircle (m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround)) {
  66.                 crouch = true;
  67.             }
  68.         }
  69.  
  70.         //only control the player if grounded or airControl is turned on
  71.         if (m_Grounded || m_AirControl) {
  72.  
  73.             // If crouching
  74.             if (crouch) {
  75.                 if (!m_wasCrouching) {
  76.                     m_wasCrouching = true;
  77.                     OnCrouchEvent.Invoke (true);
  78.                 }
  79.  
  80.                 // Reduce the speed by the crouchSpeed multiplier
  81.                 move *= m_CrouchSpeed;
  82.  
  83.                 // Disable one of the colliders when crouching
  84.                 if (m_CrouchDisableCollider != null)
  85.                     m_CrouchDisableCollider.enabled = false;
  86.             } else {
  87.                 // Enable the collider when not crouching
  88.                 if (m_CrouchDisableCollider != null)
  89.                     m_CrouchDisableCollider.enabled = true;
  90.  
  91.                 if (m_wasCrouching) {
  92.                     m_wasCrouching = false;
  93.                     OnCrouchEvent.Invoke (false);
  94.                 }
  95.             }
  96.  
  97.             // Move the character by finding the target velocity
  98.             Vector3 targetVelocity = new Vector2 (move * 10f, m_Rigidbody2D.velocity.y);
  99.             // And then smoothing it out and applying it to the character
  100.             m_Rigidbody2D.velocity = Vector3.SmoothDamp (m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
  101.  
  102.             // If the input is moving the player right and the player is facing left...
  103.             if (move > 0 && !m_FacingRight) {
  104.                 // ... flip the player.
  105.                 Flip ();
  106.             }
  107.             // Otherwise if the input is moving the player left and the player is facing right...
  108.             else if (move < 0 && m_FacingRight) {
  109.                 // ... flip the player.
  110.                 Flip ();
  111.             }
  112.         }
  113.         // If the player should jump...
  114.         if (m_Grounded && jump) {
  115.             // Add a vertical force to the player.
  116.             NumberOfJumps = 1;
  117.             m_Grounded = false;
  118.             m_Rigidbody2D.AddForce (new Vector2 (0f, m_JumpForce));
  119.         } else if (NumberOfJumps < MaxJumps && !m_Grounded && jump) {
  120.             m_Rigidbody2D.AddForce (new Vector2 (0f, m_JumpForce));
  121.             NumberOfJumps++;
  122.         }
  123.     }
  124.  
  125.     private void Flip () {
  126.         // Switch the way the player is labelled as facing.
  127.         m_FacingRight = !m_FacingRight;
  128.  
  129.         // Multiply the player's x local scale by -1.
  130.         Vector3 theScale = transform.localScale;
  131.         theScale.x *= -1;
  132.         transform.localScale = theScale;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment