[text] Code CharacterController2D

Viewer

copydownloadembedprintName: Code CharacterController2D
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. public class CharacterController2D : MonoBehaviour
  5. {
  6.         [SerializeField] private float m_JumpForce = 400f;                                                 // Amount of force added when the player jumps.
  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 Collider2D m_CrouchDisableCollider;                         // A collider that will be disabled when crouching
  14.  
  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)
  58.                                         OnLandEvent.Invoke();
  59.                         }
  60.                 }
  61.         }
  62.  
  63.  
  64.         public void Move(float move, bool crouch, bool jump)
  65.         {
  66.                 // If crouching, check to see if the character can stand up
  67.                 if (!crouch)
  68.                 {
  69.                         // If the character has a ceiling preventing them from standing up, keep them crouching
  70.                         if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
  71.                         {
  72.                                 crouch = true;
  73.                         }
  74.                 }
  75.  
  76.                 //only control the player if grounded or airControl is turned on
  77.                 if (m_Grounded || m_AirControl)
  78.                 {
  79.  
  80.                         // If crouching
  81.                         if (crouch)
  82.                         {
  83.                                 if (!m_wasCrouching)
  84.                                 {
  85.                                         m_wasCrouching = true;
  86.                                         OnCrouchEvent.Invoke(true);
  87.                                 }
  88.  
  89.                                 // Reduce the speed by the crouchSpeed multiplier
  90.                                 move *= m_CrouchSpeed;
  91.  
  92.                                 // Disable one of the colliders when crouching
  93.                                 if (m_CrouchDisableCollider != null)
  94.                                         m_CrouchDisableCollider.enabled = false;
  95.                         } else
  96.                         {
  97.                                 // Enable the collider when not crouching
  98.                                 if (m_CrouchDisableCollider != null)
  99.                                         m_CrouchDisableCollider.enabled = true;
  100.  
  101.                                 if (m_wasCrouching)
  102.                                 {
  103.                                         m_wasCrouching = false;
  104.                                         OnCrouchEvent.Invoke(false);
  105.                                 }
  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.  
  113.                         // If the input is moving the player right and the player is facing left...
  114.                         if (move > 0 && !m_FacingRight)
  115.                         {
  116.                                 // ... flip the player.
  117.                                 Flip();
  118.                         }
  119.                         // Otherwise if the input is moving the player left and the player is facing right...
  120.                         else if (move < 0 && m_FacingRight)
  121.                         {
  122.                                 // ... flip the player.
  123.                                 Flip();
  124.                         }
  125.                 }
  126.                 // If the player should jump...
  127.                 if (m_Grounded && jump)
  128.                 {
  129.                         // Add a vertical force to the player.
  130.                         m_Grounded = false;
  131.                         m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
  132.                 }
  133.         }
  134.  
  135.  
  136.         private void Flip()
  137.         {
  138.                 // Switch the way the player is labelled as facing.
  139.                 m_FacingRight = !m_FacingRight;
  140.  
  141.                 // Multiply the player's x local scale by -1.
  142.                 Vector3 theScale = transform.localScale;
  143.                 theScale.x *= -1;
  144.                 transform.localScale = theScale;
  145.         }
  146. }

Editor

You can edit this paste and save as new:


File Description
  • Code CharacterController2D
  • Paste Code
  • 20 Sep-2021
  • 4.72 Kb
You can Share it: