Removed crouching completely and added the main mechanic

This commit is contained in:
2021-07-20 18:52:15 +02:00
parent 3004249b95
commit 362b213e48
42 changed files with 118 additions and 1956 deletions

View File

@@ -4,13 +4,10 @@ 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 CapsuleCollider2D 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.
@@ -27,18 +24,12 @@ public class CharacterController2D : MonoBehaviour
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
public BoolEvent OnCrouchEvent;
private bool m_wasCrouching = false;
private void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
if (OnLandEvent == null)
OnLandEvent = new UnityEvent();
if (OnCrouchEvent == null)
OnCrouchEvent = new BoolEvent();
}
private void FixedUpdate()
@@ -61,50 +52,13 @@ public class CharacterController2D : MonoBehaviour
}
public void Move(float move, bool crouch, bool jump)
public void Move(float move, 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

View File

@@ -7,12 +7,12 @@ public class PlayerControls : MonoBehaviour
public Animator animator;
public PhysicsMaterial2D top;
public Rigidbody2D rigidbody;
public SpriteRenderer sprite;
public PhysicsMaterial2D bottom;
public CharacterController2D controller;
public float runspeed = 40f;
float horizonatalaxis = 0f;
bool jumping = false;
bool crouching = false;
bool switchy = false;
void Start()
@@ -26,7 +26,6 @@ public class PlayerControls : MonoBehaviour
horizonatalaxis = Input.GetAxisRaw("Horizontal");
jumping = Input.GetButton("Jump") || Input.GetAxisRaw("Vertical") > 0;
crouching = Input.GetButton("Crouch") || Input.GetAxisRaw("Vertical") < 0;
animator.SetFloat("Horizontal_speed", horizonatalaxis);
if(Input.GetButtonDown("Switch")) {
@@ -34,21 +33,17 @@ public class PlayerControls : MonoBehaviour
}
if (switchy){
transform.rotation = new Quaternion(0f, 0f, 180f, 0f);
sprite.flipY = true;
rigidbody.sharedMaterial = top;
}
else {
transform.rotation = new Quaternion(0f, 0f, 0f, 0f);
sprite.flipY = false;
rigidbody.sharedMaterial = bottom;
}
}
public void crouchingx(bool stat){
//animator.SetBool("Crouching", stat);
}
void FixedUpdate(){
controller.Move(horizonatalaxis * runspeed * Time.fixedDeltaTime, false, jumping);
controller.Move(horizonatalaxis * runspeed * Time.fixedDeltaTime, jumping);
}
}