Any way to write the following code using the new input system? (ie OnJump)

void Jump(){
        bool isGrounded = myCol.IsTouchingLayers(LayerMask.GetMask("Ground"));
        if(!jumped && Input.GetKeyDown("space") && isGrounded){
            
            myRigidbody.AddForce(new Vector2(0,jumpForce*100));
            jumped = true;
            
           
        }
        if (isGrounded){
            myAnimator.SetBool("isJumping",false);
            
        }
        else if (!isGrounded && jumped){
            myAnimator.SetBool("isJumping",true);
            jumped = false;
        }
        
    }

Because, if im correct, OnJump would only be called when i press spacebar so it wont be able to know when the player touches the ground again

Hi,

Yes, you are right. Given you bound your Jump action to the space key, “OnJump” should get called when you press the space key. The rest of your code does not have anything to do with user input, so it’ll probably work inside the OnJump method as it is.

Give it a try! :slight_smile:

1 Like

In OnJump you will check if you are grounded and if not, do nothing. If you are grounded, you can set your animation and apply the force.

In Update you will have to continuously do your isGrounded check. This would also have to be a class-scoped variable so that you can check it in OnJump

Some quick psuedo-code

isGrounded = true;

Update
    wasGrounded = isGrounded
    isGrounded = touching the ground?
    if I was not grounded, but now I am (if !wasGrounded && isGrounded)
        reset animation

OnJump
    if not isGrounded
        return

    set jump animation
    apply force
1 Like

I will try this. Btw are there really any benefits of using the input system instead of what i did

I can’t really say because I don’t use it very often. This is only because I end up not releasing anything I make. I am bad that way. If I was releasing anything, I’d probably go with it. It’s a lot more flexible to use when you are trying to cater for multiple inputs.

As bixarrio wrote, the new input system offers a lot of flexibility. If you don’t want to allow your future player to, for example, remap their keys and if you don’t plan anything more complex than “press space and jump”, using the old input system is perfectly fine.

If you plan to keep developing and maintaining this game for years, switching to the new input system is probably a good idea because you cannot know if/when Unity will drop the old input system. In that case, you would evaluate whether it requires more effort and time to replace the code now or in the future. If you just have a few things such as “run if A or D was pressed”, it does not matter. If your game becomes more complex in regards to user input, it’s probably better to not waste your time with the old input system.

As a game developer, these are questions you have to ask yourself because you know your game and your plans best.


See also:

1 Like

Oh. I hope you end up releasing a banger soon

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms