0
\$\begingroup\$

I made a simple game in unity where the user taps their phone screen and the player jumps up. When I test it on my phone (not sure if this matters but it's a Samsung s9+), I tap the screen and usually the tap causes the player to jump up and fall back to the platform properly, but sometimes when I tap the screen nothing happens at all it just stays in place. The game on my phones is also very slightly laggy (although, on my computer it runs smoothly) and I'm not sure how to fix that either or if they're related.

This is the code attached to the player:

 void FixedUpdate()
 {
     Jump();
     Dead();
 }

 void Jump()
 {
     if (Input.touchCount > 0 && isGrounded)
     {
         gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce * Time.fixedDeltaTime), ForceMode2D.Impulse);
     }
 }

Also, I know that it's the touch input that's the problem, because when I make it so that pressing the up arrow key on the keyboard makes the player jump, it works perfectly fine. If there's some other information about the game you'd need to know to answer my question, let me know. Thanks for your help.

\$\endgroup\$
1
\$\begingroup\$

Your main problem is that you're checking for input in FixedUpdate.

FixedUpdate is not guaranteed to run every frame, or only once per frame. It will run however many times the physics simulation needs to "catch up" to the current frame time, acting in fixedTimestep intervals.

So by handling input here, you run the risk of skipping or double-handling inputs if you're not careful. The simplest fix is to move your input detection code into Update instead

Your second problem is that you're integrating your impulse over time - an impulse like a jump should be an instantaneous push, that doesn't vary in scale depending on how long your simulation step is. So don't multiply by deltaTime here.

You may also want to apply rigidbody interpolation to help with the perceived lag/choppiness. This can come from the mismatch between the physics sim time and the display time, making it look like your game is running at a lower framerate than it is. Turning on interpolation smoothes this out so you don't see juddering.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.