Character doesn't jump

I’m getting a weird bug in the way my Rigidbody2D is interacting with my InputSystem.

The OnJump() method I wrote seems to work just fine, except that it isn’t changing the velocity of playerRigidbody. The character moves left and right just fine, and the Debug.Log I embedded in OnMove() tells me that I’m getting the expected values for playerRigidbody.velocity. However, the player doesn’t jump, and the Info section of Rigidbody2D in the inspector says that velocity.y never changes.

Here’s a better screenshot of the gameplay that shows the aforementioned inspector data.

It’s because the movement is removing any velocity you added for the jump.

Instead of setting the velocity in jump, create a jump vector the same way you did for movement. Then, in your Run method, combine the two vectors and use that to set the velocity. Or just set a flag.

private bool jumping = false;
private void OnJump(InputValue value)
{
    if (value.isPressed)
    {
        jumping = true;
    }
}

private void Run()
{
    Vector2 myVelocity = new Vector2(moveInput.x * mySpeed, 0f);
    if (jumping)
    {
        myVelocity.y = jumpSpeed;
    }
    playerRigidbody.velocity = myVelocity;
}

That’s no good. The jump vector approach results in an infinite jump, while the flag approach produces an infinite jump or a micro jump, depending upon where I reset the flag.

Even more maddening is the fact that I’m re-visiting the lecture as a refresher, and last time I had no trouble with my implementation of Rick’s solution. I could just fork that original project and rip out all the stuff that isn’t relevant to the current project, but that would be a frustrating time sink that doesn’t teach me anything.

You said that in my implementation I was removing any velocity added for the jump, but I don’t see where that is happening.

Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

From what I see in your screenshot, the OnJump methods adds a vector to the current vector each time the ‘jump’ key gets pressed. The reason for that is +=. There is no restriction (yet) which prevents the player from ‘jumping’ multiple times per second.

Since your jumpSpeed value is fairly high, the problem is very obvious in your game. Rick has got the same problem, though, but it is less obvious.

If you want, you could try to solve this problem yourself. You just have to implement a restriction. If you think about it in a logical way and use Debug.Logs to analyse the situation, the solution is rather obvious. The implementation of the solution might be a bit more challenging, though. If you cannot figure it out yourself, please keep watching. Alternatively, you could take a look at Rick’s repository.

Hope this helps. :slight_smile:


See also:

In run, you set myVelocity = new Vector2(moveInput.x * moveSpeed, 0f);. That’s where it is happening. You set any y-velocity you may have added in the jump equal to 0. The update loop runs much more frequent than the physics loop, so that would almost certainly clear any y-velocity before the physics loop gets a chance to run.

My example wasn’t tested at all, I wrote it here in the post. I didn’t reset the jumping flag - which is a bug, I meant to clear it in run. But it’s wrong anyway. That would apply some velocity for 1 frame and on the very next frame, it will remove it again. What you should probably do is grab the y-velocity from the rigidbody and keep it. Then, only add to it when you jump. The code is much the same with a small change

private bool jumping = false;
private void OnJump(InputValue value)
{
    if (value.isPressed)
    {
        jumping = true;
    }
}

private void Run()
{
    // get movement, but preserve current y-velocity
    Vector2 myVelocity = new Vector2(moveInput.x * mySpeed, playerRigidbody.velocity.y);
    if (jumping)
    {
        // update y-velocity to jump
        myVelocity.y = jumpSpeed;
        jumping = false;
    }
    playerRigidbody.velocity = myVelocity;
}

This will work as long as gravity is turned on. Preserving the y-velocity will allow the gravity to bring the player back down. Also, I wrote this in post, too, so it’s once again not tested

Privacy & Terms