Question by
GabrielTheSnake · Dec 27, 2020 at 08:25 PM ·
c#jumpbeginner
How to make a Hollow Knight/Mario style jump in C#?
I'm trying to make a system where you can hold the jump key to jump higher. currently, the longer I press the button, the higher the player jumps but when i release the key he keeps going up for a while. I want it to be like Hollow Knight or Mario where you start falling as soon as you release the jump key. can anyone help me?
public class PlayerController : MonoBehaviour
{
public float jumpForce;
private Rigidbody2D rb;
private bool isGrounded;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
void Update()
{
if(isGrounded == true && Input.GetKeyDown(KeyCode.W))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if(Input.GetKey(KeyCode.W) && isJumping == true)
{
if(jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.W) && !wallSliding)
{
isJumping = false;
}
}
}
Comment