Hi to everyone,
I’m doing a 2D platformer and i’m adding the high jump while crouching mechanic (the same as in Super Mario 64) To do this the player has to press the down arrow (crouch) and then the jump button. The problem comes when you are in the air after pressing this combination. If you keep pressing the down arrow and the jump button, you cannot move from side to side. But in the moment you realease one of them, unity allows you to move again. I’ve been checking my code to see if I limited the movement in those scenarios, and I didn’t.
I hope the problem comes from some way of the unity controll system that I didn’t know about, and not from me being blind and don’t seing something obvious.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static Unity.Burst.Intrinsics.X86.Avx;
public class Player : MonoBehaviour
{
[SerializeField]
StaminaManagement stamina;
private float horizontal;
private float vertical;
public float speed = 8f;
public float jumpingPower = 16f;
public float highJumpingPower = 24f;
private bool isFacingRight = true;
private bool isWallSliding;
public float wallSlidingSpeed = 2f;
private bool isWallJumping;
private float wallJumpingDirection;
private float wallJumpingTime = 0.2f;
private float wallJumpingCounter;
private float wallJumpingDuration = 0.2f;
private Vector2 wallJumpingPower = new Vector2(12f, 16f);
private bool canDash = true;
private bool isDashing;
private float dashingPower = 20f;
private float dashingTime = 0.2f;
private float dashingCooldown = 1f;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private Transform wallCheck;
[SerializeField] private LayerMask wallLayer;
private void Update()
{
if (isDashing)
{
return;
}
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
Jump();
HighJump();
WallSlide();
WallJump();
ConsumeFood();
if (Input.GetButtonDown("Fire3") && stamina.GetCurrentStamina() >= stamina.GetDashStaminaCost()) {
stamina.DashStaminaLos();
StartCoroutine(Dash());
}
if (!isWallJumping)
{
Flip();
}
}
private void FixedUpdate()
{
if (isDashing)
{
return;
}
Walk();
}
private IEnumerator Dash() {
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0;
rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
yield return new WaitForSeconds(dashingTime);
rb.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
private void Walk(){
if (!isWallJumping && !IsCrouching())
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
if (IsCrouching())
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
}
private void Jump() {
if (Input.GetButtonDown("Jump") && IsGrounded() && !IsCrouching())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
}
private void ConsumeFood() {
if (Input.GetKeyDown(KeyCode.Z)) {
stamina.ConsumeFood();
}
}
private void HighJump() {
if (Input.GetButtonDown("Jump") && IsCrouching() && stamina.GetCurrentStamina() >= stamina.GetHighJumpStaminaCost())
{
stamina.HighJumpStaminaLoss();
rb.velocity = new Vector2(rb.velocity.x, highJumpingPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
}
private bool IsGrounded()
{
return Physics2D.OverlapBox(groundCheck.position, new Vector2(0.98f, 0.1f) ,0f, groundLayer);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 1, 0, 0.75F);
Gizmos.DrawCube(groundCheck.position, new Vector2(0.98f, 0.1f));
}
private bool IsCrouching(){
if (vertical == -1 && IsGrounded())
{
return true;
}
else return false;
}
private bool IsWalled()
{
return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
}
private void WallSlide()
{
if (IsWalled() && !IsGrounded())
{
isWallSliding = true;
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
else
{
isWallSliding = false;
}
}
private void WallJump()
{
if (isWallSliding)
{
isWallJumping = false;
wallJumpingDirection = -transform.localScale.x;
wallJumpingCounter = wallJumpingTime;
CancelInvoke(nameof(StopWallJumping));
}
else
{
wallJumpingCounter -= Time.deltaTime;
}
if (Input.GetButtonDown("Jump") && wallJumpingCounter > 0f && stamina.GetCurrentStamina() >= stamina.GetWallJumpStaminaCost())
{
stamina.WallJumpStaminaLoss();
isWallJumping = true;
rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
wallJumpingCounter = 0f;
if (transform.localScale.x != wallJumpingDirection)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
Invoke(nameof(StopWallJumping), wallJumpingDuration);
}
}
private void StopWallJumping()
{
isWallJumping = false;
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
}
I’m not used to ask for help in forums, so idk if posting the entire class is the way to go, or if you preffer some other aproach. Let me know if there is any problem with the way I asked the question.