Home > Software design >  Unity mobile hold jump = jump higher
Unity mobile hold jump = jump higher

Time:10-04

I'm trying to make a mobile sidescroller and I want the player to be able to jump higher if the jump UI button is held down, I could not find any toturials on youtube for mobile and I'm reletively new to scripting so I tried to do it by myself but it did not work properly, the only toturials I can find are for pc and I can't implement them correctly for mobile because they require Input.getbuttondown and inputs like that impossible for mobile, this is my current script, the jump ui button has event trigger

    public void Jump()
{
    if (isGrounded())
    {
        jumping = true;
        animator.SetBool("NowJumping", true);
        SoundManager.PlaySound("PlayerJump");
        rb.velocity = new Vector2(rb.velocity.x, jumpPower);

    if (jumping)
    {
        rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    }
}

public void StopJump()
{
    jumping = false;
}

the jump is always boosted by the if (jumping) velocity boost, I'm out of ideas on how to put this feature in my game, anyone have any ideas how to fix this? Thank you very much in advance

CodePudding user response:

I would start coroutine when jumping starts. Like 0.5 secs long. After coroutine ends, it makes the jumping false and stops jumping even if you keep pushing the button down. I am in my phone, so i can't test this. I suppose while you are pressing the button down, it always makes the jumping true because of the way you designed your code. So to make it work, you can make jumping true when key is pressed the first time (not holding down). Then in OnButtonHeldDown() you can make if (jumping) {bla bla}. This way when coroutine ends it makes jumping false and jumping would not be true again while pressing button. Player needs to release and press again to make jumping true. And like you wrote, it cannot release button and press again the trick the code, because only when it is grounded it can jump. I hope i managed to explain my reasoning.

Also you can make the button uninteractible after 0.5 secs and make in interactible again after the character is grounded.

For coroutine usage, you can check here.

https://docs.unity3d.com/Manual/Coroutines.html

CodePudding user response:

Set jumping to false using OnPointerUp. You can read how to use it here

https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.Selectable.OnPointerUp.html

  • Related