Problem with input system

Hi ,
I have a problem with the jump section.
I wrote this code for jump and did everything Rick said.

void OnJump(InputValue value)
    { 
        if (value.isPressed)
        {
            KingRB2d.velocity += new Vector2(0f , 0.5f);
        }  
    }

But when I hit space, my object keeps going up.
In fact, in each frame, the Y value increases.
Where did I go wrong?

tnx.

Hi Rezixo,

Welcome to our community! :slight_smile:

How often does OnJump get called? And have you already looked up the definition of isPressed? Maybe you need a different condition.


See also:

Thank you.

When I press the Space key, the OnJump is called.
There is no problem with identifying the space. I checked it.

I think the problem is the Jump method.
I used two methods and they both had this problem.

void Jump()
    {
        KingRB2d.AddForce(Vector2.up * jumpSpeed , ForceMode2D.Impulse);
    }
void Jump()
    {
        KingRB2d.velocity += new Vector2(0f , jumpSpeed );;
    }

This is the problem:

Do you have any idea about this?

Thanks.

Is there any restriction for the jumping? If not, this behaviour is expected because the velocity gets increased each frame while the key is being pressed. Rick solves this problem in lecture “Jump If IsTouchingLayers”. Have you already watched it?

I watched that video but it still not working.
I changed my code to this :

void OnJump(InputValue value)
    {
        if(KINGcollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            return;
        }
        if (value.isPressed)
        {
            KingRB2d.velocity += new Vector2(0f , jumpSpeed );
        }
    }

I don’t understand what you mean by the Restriction. Can you explain more?

By restriction, I meant an if-statement which prevents code from being executed in certain cases. In our case, we want to jump only if the player is touching the ground.

In your case, the condition is: “If the player is touching the ‘ground’ layer”. And the code that is supposed to be executed: “Terminate the method immediately.”

return; terminates the method immediately.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

I just compared my code and Lecture Project Changes, they were no different!

I tried to find the problem in another way.
I created a new project. Then I set the jump with the input system in the same way as before.
In disbelief , it worked!!!

So I deleted the Player game object and rebuilt it.
Finally the problem was solved!
But I still don’t know where that problem came from :joy:

Thanks for helping me Nina :orange_heart:

I’m glad recreating the player game object helped. In rare cases, the Unity components are buggy.

1 Like

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

Privacy & Terms