Unity 2D - Need Help Staying Crouched while Under Something
Howdy!
So my question is simple; I'm building a 2D platformer and I'm trying to simply implement some functionality that will help players have a better game feel, and possibly prevent bugs. All I'd like to do is to check if something is above my character and keep him crouching if I'm underneath something. The moment I come out from under said thing, I'd like to stand back up (if not holding crouch).
Normally, I have players hold Left Control to crouch, and the character stands when it's released. I'd like to know how to keep the character crouched if under something, and especially if under something with the button released. Then simply spring upward when out from under said thing.
Any help is appreciated and I'll include my character controller for reference. Oh, and I guess it'd also be helpful to mention that I'm using Unity 2019.1.4f1. If I need to upgrade, I can; just thought I'd clarify.
Thanks guys!
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerPlatformerController : PhysicsObject { public float maxSpeed = 10; public float jumpTakeOffSpeed = 20; public float fallMultiplier = 2.5f; //from tutorial; jumping feels wonky because gravity isn't speeding up fall. public float lowJumpMultiplier = 2f; //for when you don't jump all the way BoxCollider2D playerBoxCollider; ParticleSystem particles; Rigidbody2D rb; ParticleSystem ps; private SpriteRenderer spriteRenderer; private Animator animator; // Use this for initialization void Awake() { playerBoxCollider = GameObject.Find("PlayerObject").GetComponent<BoxCollider2D>(); particles = GameObject.Find("PlayerParticles").GetComponent<ParticleSystem>(); spriteRenderer = GetComponent<SpriteRenderer>(); animator = GetComponent<Animator>(); rb = GetComponent<Rigidbody2D>(); } protected override void ComputeVelocity() { Vector2 move = Vector2.zero; move.x = Input.GetAxis("Horizontal"); if (Input.GetButtonDown("Jump") && grounded) { velocity.y = jumpTakeOffSpeed + 3f; } else if (Input.GetButtonUp("Jump")) { if (rb.velocity.y < 0) { rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime; } if (velocity.y > 0) { velocity.y = velocity.y * 0.5f; rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime; } } bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.0f) : (move.x < 0.0f)); if (flipSprite) { spriteRenderer.flipX = !spriteRenderer.flipX; maxSpeed = 10; } animator.SetBool("grounded", grounded); animator.SetBool("crouchEngaged", crouchEngaged); animator.SetFloat("velocityX", Mathf.Abs (velocity.x) / maxSpeed); targetVelocity = move * maxSpeed; if ((move.x != 0.0f) && !grounded) //if set as (move.x > 0) it works only running left, but not right { particles.Play(true); } else if (velocity.x == 0.0f) { particles.Play(false); } if (Input.GetKey(KeyCode.LeftControl)) { playerBoxCollider.enabled = false; maxSpeed = 4; crouchEngaged = true; } else if (Input.GetKeyUp(KeyCode.LeftControl)) { crouchEngaged = false; playerBoxCollider.enabled = true; maxSpeed = 10; } if (Input.GetKey(KeyCode.LeftShift)) { if(maxSpeed < 20) { maxSpeed += 3 * Time.deltaTime; } } if (Input.GetKeyUp(KeyCode.LeftShift)) { maxSpeed = 10; } Debug.Log(maxSpeed); } // research needs to be done on this function. When going under, it will remain crouched (which is good) // however, once you need to "stand" the animation never happens. /*bool Stand() { //work in progress }*/ }