- Home /
Jumping by using unity input system
hi, I have a jumping script the longer you press the higher it jumps I have tried to implement it into the unity input system but some bugs were happening like it was sliding in air.
here is the code for the unity input system:
public void Jump(InputAction.CallbackContext context)
{
if (stun == false)
{
if (jumpTimer > Time.time && onGround && context.performed)
{
animator.SetTrigger("takeOf");
Jump();
}
else
{
animator.SetBool("isJumping", true);
}
}
else
{
rb.gravityScale = gravity;
rb.drag = linearDrag * 0.15f;
if (rb.velocity.y < 0)
{
rb.gravityScale = gravity * fallMultiplier;
}
else if (rb.velocity.y > 0 && context.canceled)
{
rb.gravityScale = gravity * (fallMultiplier / 2);
}
}
}
original code:
void FixedUpdate()
{
if (jumpTimer > Time.time && onGround)
{
animator.SetTrigger("takeOf");
Jump();
}
else
{
animator.SetBool("isJumping", true);
}
modifyPhysics();
}
void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, 0);
rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
jumpTimer = 0;
}
void modifyPhysics()
{
if(!onGround)
{
rb.gravityScale = gravity;
rb.drag = linearDrag * 0.15f;
if (rb.velocity.y < 0)
{
rb.gravityScale = gravity * fallMultiplier;
}
else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
{
rb.gravityScale = gravity * (fallMultiplier / 2);
}
}
}
Answer by Lenny991 · Mar 05 at 12:31 PM
I was actually JUST trying to do this, here is what i have so far.
if (Input.GetKeyDown("Space") && onGround)
{
onGround = false;
Jump();
}
else if (rb.velocity.y > 4 && Input.GetKeyDown("Space"))
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * .9f);
the bottommost line is what does the jump modifying, i just have this in update.
the script isn't prefect yet but i think it is pretty good, i havent found any big issues with it.
hey, I don't think you got my issue correctly the code works perfectly but I wanted to implement it on unity input system by using this function public void Jump(InputAction.CallbackContext context)
so the game can be playable in local multiplayer, I hope that I explained my issue well
ohhh, i haven't used the new input system yet in any project, im sorry.
but the basic concept of it is
if the y velocity is more than 4, and the player stops pressing the jump button, the jump's height is reset.
i am sorry i can't help but i very well think you can definitely use this system using the new input system