Advertisement
Guest User

Untitled

a guest
Jan 17th, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. public class CharacterController : MonoBehaviour
  5. {
  6.     public float m_JumpForce = 400f;
  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.     public bool m_AirControl = false;                         // Whether or not a player can steer while jumping;
  10.     public LayerMask m_WhatIsGround;                          // A mask determining what is ground to the character
  11.     public Transform m_GroundCheck;                           // A position marking where to check if the player is grounded.
  12.     public Transform m_CeilingCheck;                          // A position marking where to check for ceilings
  13.     public 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.     public 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, bool jetpack)
  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.             }
  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.             // Add a vertical force to the player.
  131.             m_Grounded = false;
  132.             m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
  133.         }
  134.  
  135.         if (jetpack)
  136.         {
  137.             // Add a vertical force to the player.
  138.             m_Grounded = false;
  139.             m_Rigidbody2D.AddForce(new Vector2(0f, (m_JumpForce / 2f)));
  140.         }
  141.     }
  142.  
  143.  
  144.     private void Flip()
  145.     {
  146.         // Switch the way the player is labelled as facing.
  147.         m_FacingRight = !m_FacingRight;
  148.  
  149.         // Multiply the player's x local scale by -1.
  150.         Vector3 theScale = transform.localScale;
  151.         theScale.x *= -1;
  152.         transform.localScale = theScale;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement