diff --git a/Assets/Physic_Materials/NotSlippery.physicsMaterial2D b/Assets/Physic_Materials/Slippery.physicsMaterial2D similarity index 91% rename from Assets/Physic_Materials/NotSlippery.physicsMaterial2D rename to Assets/Physic_Materials/Slippery.physicsMaterial2D index 7d10316..1c8e6b2 100644 --- a/Assets/Physic_Materials/NotSlippery.physicsMaterial2D +++ b/Assets/Physic_Materials/Slippery.physicsMaterial2D @@ -6,6 +6,6 @@ PhysicsMaterial2D: m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_Name: NotSlippery + m_Name: Slippery friction: 1 bounciness: 0 diff --git a/Assets/Physic_Materials/NotSlippery.physicsMaterial2D.meta b/Assets/Physic_Materials/Slippery.physicsMaterial2D.meta similarity index 100% rename from Assets/Physic_Materials/NotSlippery.physicsMaterial2D.meta rename to Assets/Physic_Materials/Slippery.physicsMaterial2D.meta diff --git a/Assets/Scripts/CharacterController2D.cs b/Assets/Scripts/CharacterController2D.cs new file mode 100644 index 0000000..925bfe0 --- /dev/null +++ b/Assets/Scripts/CharacterController2D.cs @@ -0,0 +1,146 @@ +using UnityEngine; +using UnityEngine.Events; + +public class CharacterController2D : MonoBehaviour +{ + [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps. + [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100% + [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; // How much to smooth out the movement + [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping; + [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character + [SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded. + [SerializeField] private Transform m_CeilingCheck; // A position marking where to check for ceilings + [SerializeField] private Collider2D m_CrouchDisableCollider; // A collider that will be disabled when crouching + + const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded + private bool m_Grounded; // Whether or not the player is grounded. + const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up + private Rigidbody2D m_Rigidbody2D; + private bool m_FacingRight = true; // For determining which way the player is currently facing. + private Vector3 m_Velocity = Vector3.zero; + + [Header("Events")] + [Space] + + public UnityEvent OnLandEvent; + + [System.Serializable] + public class BoolEvent : UnityEvent { } + + public BoolEvent OnCrouchEvent; + private bool m_wasCrouching = false; + + private void Awake() + { + m_Rigidbody2D = GetComponent(); + + if (OnLandEvent == null) + OnLandEvent = new UnityEvent(); + + if (OnCrouchEvent == null) + OnCrouchEvent = new BoolEvent(); + } + + private void FixedUpdate() + { + bool wasGrounded = m_Grounded; + m_Grounded = false; + + // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground + // This can be done using layers instead but Sample Assets will not overwrite your project settings. + Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround); + for (int i = 0; i < colliders.Length; i++) + { + if (colliders[i].gameObject != gameObject) + { + m_Grounded = true; + if (!wasGrounded) + OnLandEvent.Invoke(); + } + } + } + + + public void Move(float move, bool crouch, bool jump) + { + // If crouching, check to see if the character can stand up + if (!crouch) + { + // If the character has a ceiling preventing them from standing up, keep them crouching + if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround)) + { + crouch = true; + } + } + + //only control the player if grounded or airControl is turned on + if (m_Grounded || m_AirControl) + { + + // If crouching + if (crouch) + { + if (!m_wasCrouching) + { + m_wasCrouching = true; + OnCrouchEvent.Invoke(true); + } + + // Reduce the speed by the crouchSpeed multiplier + move *= m_CrouchSpeed; + + // Disable one of the colliders when crouching + if (m_CrouchDisableCollider != null) + m_CrouchDisableCollider.enabled = false; + } else + { + // Enable the collider when not crouching + if (m_CrouchDisableCollider != null) + m_CrouchDisableCollider.enabled = true; + + if (m_wasCrouching) + { + m_wasCrouching = false; + OnCrouchEvent.Invoke(false); + } + } + + // Move the character by finding the target velocity + Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y); + // And then smoothing it out and applying it to the character + m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing); + + // If the input is moving the player right and the player is facing left... + if (move > 0 && !m_FacingRight) + { + // ... flip the player. + Flip(); + } + // Otherwise if the input is moving the player left and the player is facing right... + else if (move < 0 && m_FacingRight) + { + // ... flip the player. + Flip(); + } + } + // If the player should jump... + if (m_Grounded && jump) + { + // Add a vertical force to the player. + m_Grounded = false; + m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); + } + } + + + private void Flip() + { + // Switch the way the player is labelled as facing. + m_FacingRight = !m_FacingRight; + + // Multiply the player's x local scale by -1. + Vector3 theScale = transform.localScale; + theScale.x *= -1; + transform.localScale = theScale; + } +} diff --git a/Assets/Scripts/CharacterController2D.cs.meta b/Assets/Scripts/CharacterController2D.cs.meta new file mode 100644 index 0000000..d7e5e10 --- /dev/null +++ b/Assets/Scripts/CharacterController2D.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8057fdb619b42d94390909e7bc867d87 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerControls.cs b/Assets/Scripts/PlayerControls.cs index 5e56d9c..9abe5fc 100644 --- a/Assets/Scripts/PlayerControls.cs +++ b/Assets/Scripts/PlayerControls.cs @@ -4,12 +4,11 @@ using UnityEngine; public class PlayerControls : MonoBehaviour { - public Rigidbody2D rb; - public CircleCollider2D circ; - public LayerMask LVLMask; - public float walking_speed; - public float jump_power; - // Start is called before the first frame update + public CharacterController2D controller; + public float runspeed = 40f; + float horizonatalmove = 0f; + bool jumping = false; + bool crouching = false; void Start() { @@ -18,17 +17,14 @@ public class PlayerControls : MonoBehaviour // Update is called once per frame void Update() { - float speed = Input.GetAxisRaw("Horizontal") * Time.deltaTime; - Vector2 movement = new Vector2(0, 0); - movement.x = walking_speed * speed; + horizonatalmove = Input.GetAxisRaw("Horizontal") * runspeed; + jumping = Input.GetButton("Jump") || Input.GetAxisRaw("Vertical") > 0; + crouching = Input.GetButton("Crouch") || Input.GetAxisRaw("Vertical") < 0; - if (Input.GetButtonDown("Jump") || Input.GetAxisRaw("Vertical") > 0) - { - if (circ.IsTouchingLayers(LVLMask)){ - movement.x = movement.x / 10; - movement.y = jump_power; - } - } - rb.AddRelativeForce(movement); + } + + void FixedUpdate(){ + controller.Move(horizonatalmove * Time.fixedDeltaTime, crouching, jumping); + } }