0
\$\begingroup\$

What I want to do is to cut a jump midair by turning off the y velocity when the player releases the jump button (like in Hollow Knight). I managed to do that with:

rb.velocity = new Vector2(rb.velocity.x, 0f); 

My problem is when I cut the jump and I am midair, if I repeat the release jump button process, the whole thing happens again, the character staggers as long as I am releasing the jump button continously.

I would like to be able to do that only once when I am off the ground. I am a noob when it comes to coding so I don't really know what to do.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 
 public class PlayerController : MonoBehaviour {
 
     public Animator animator;
 
     private Rigidbody2D rb;
     public float speed;
     private float moveInput;
     public float jumpForce;
 
     private bool isGrounded;
     public Transform feetPos;
     public float checkRadius;
     public LayerMask whatIsGround;
 
     private float jumpTimeCounter;
     public float jumpTime;
     private bool isJumping;
     private bool facingRight = true;
     public float jumpDownForceY = 0;
 
     
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
 
     }
 
     void Update()
     {
 
         isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
 
         if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
             rb.AddForce(Vector2.up * 700f);
 
         if (Mathf.Abs(moveInput) > 0 && rb.velocity.y == 0)
             animator.SetBool("Running", true);
         else
             animator.SetBool("Running", false);
 
         if (rb.velocity.y == 0)
         {
             animator.SetBool("Jumping", false);
             animator.SetBool("Falling", false);
         }
 
         if (rb.velocity.y > 0.2)
         {
             animator.SetBool("Jumping", true);
         }
             
 
         if (rb.velocity.y < 0)
         {
             animator.SetBool("Jumping", false);
             animator.SetBool("Falling", true);
         }
 
         if (isGrounded == true && Input.GetButtonDown("Jump"))
         {
             isJumping = true;
             
             jumpTimeCounter = jumpTime;
             rb.velocity = Vector2.up * jumpForce;
         }
 
         if (Input.GetButton("Jump") && isJumping == true)
         {
 
             if (jumpTimeCounter > 0){
                 rb.velocity = Vector2.up * jumpForce;
                 jumpTimeCounter -= Time.deltaTime;
             } else {
                 isJumping = false;
             }
 
            
         }
 
         if (Input.GetButtonUp("Jump"))
         {
             isJumping = false;
             rb.velocity = new Vector2(rb.velocity.x, 0f);
 
         }
 
     }
 
     void FixedUpdate()
     {
         moveInput = Input.GetAxisRaw("Horizontal");
         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
         Flip(moveInput);
 
     }
 
     private void Flip(float moveInput)
     {
         if (moveInput > 0 && !facingRight || moveInput < 0 && facingRight)
         {
             facingRight = !facingRight;
             Vector3 theScale = transform.localScale;
             theScale.x *= -1;
             transform.localScale = theScale;
         }
 
 
     }
 }
\$\endgroup\$
6
  • \$\begingroup\$ Are you trying to make the player not jump while in the air? \$\endgroup\$
    – Naor Hadar
    Feb 9 at 13:00
  • \$\begingroup\$ On Jump ButtonUp (button release) I want the player to stop going up, and remove y velocity in the process. I achieved that but when i release the button again while in air the character staggers and repeats the whole loop. \$\endgroup\$
    – Bluebear
    Feb 9 at 13:19
  • \$\begingroup\$ So as I said, the problem is that you are jumping in mid-air. You just need to check if the player is on the ground, before jumping, not checking its y-velocity (while that can be as well). \$\endgroup\$
    – Naor Hadar
    Feb 9 at 13:21
  • \$\begingroup\$ Here are some possible solutions in Unity: answers.unity.com/questions/196381/… \$\endgroup\$
    – Naor Hadar
    Feb 9 at 13:23
  • \$\begingroup\$ Hi there, doesn't this happen because if you release jump, you cut off the velocity by setting it to 0f, but if you press Jump again while it's still in the air, you don't jump but your velocity cut-off code still executes, basically resetting it back to 0f. You have quite some checks already and I don't know if that's all necessary, but I think if you would check in that if (Input.GetButtonUp("Jump")) (where you reset the velocity) if the y velocity is NOT negative (falling down), it should't keep resetting the velocity and thus not cause stagger anymore. \$\endgroup\$
    – D.Kallan
    Feb 9 at 14:19
1
\$\begingroup\$

Solved it by checking if the button is up AND the y velocity is more than zero. Basically the velocity code will not be executed when the character is falling. Here is the code snippet:

if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
    {
        isJumping = false;
        rb.velocity = new Vector2(rb.velocity.x, 0f);

    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.