- Home /
Different Jump Heights: Input.GetKey vs Pointer Down
Hi,
I am creating a very simple platformer and have noticed a very strange behavior when playing it on Android: while testing on PC, my obstacles were very easy to overcome but once on my phone they were almost impossible to beat. For PC controls, I am using LeftArrow, RightArrow and Space. On Android I am using 3 UI elements and Pointer Down events. I did some experiments and added an UI element to display the max height of my character after each jump and this confirmed my expectations: 2.218 on PC and 1.834 on Android (or on PC using the UI to jump) If I set a multiplier of 1.145f on the UI jump, then the jump height is almost the same. Why is the jump height different when it should be the same for both methods??
if (Input.GetKey(KeyCode.Space))
{
if (onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
boink.Play();
}
}
if (jump)
{
if (onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight * 1.145f);
boink.Play();
}
jump = false;
}
Answer by hexagonius · Sep 27, 2018 at 07:10 PM
use the Input.GetKeyDown method to achieve the same effect.
thaks a lot bro, that fixed it for me! any idea why it is like this? also: any way to make my Ponter Down the same as Input.Get$$anonymous$$ey?
If you use Get$$anonymous$$eyDown
method (or the UI), the code setting the vertical velocity will execute exactly once - for Get$$anonymous$$eyDown
it will be during the frame in which Space
press was detected, for the UI in will be in the frame in which jump
is detected to be true.
If you use Get$$anonymous$$ey
, it will be executed during more than one frame - for as long as you press the Space
key and your character is still marked as onGround
. This means there will be some frames during which your character would be already slowing down due to gravity, but you're resetting its vertical velocity to jumpHeight
. Place a Debug.Log("Jump!");
before rb.velocity = new Vector2(rb.velocity.x, jumpheight);
and see how many times it's setting the speed per "single" space press. Even if you press and release the space key very quickly, there will be more than one "Jump" in your console.
Also, if your question was answered and you want it to be closed, mark hexagonius' answer as the correct one.
hexagonius ·
use the Input.Get$$anonymous$$eyDown method to achieve the same effect.
--> closed
Your answer
Follow this Question
Related Questions
Windows 8 / Store App: Inputs (WM_POINTER) 0 Answers
IPointerEnterHandler / IPointerExitHandler and touch 1 Answer
IPointerEnterHandler doesn't work when object switches parent 1 Answer
How to reset IsPointerOverGameObject(-1) after click? 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers