Advertisement
Guest User

Character Controller Script

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