GetKeyDown breaks jumping vs GetKey
Hi all,
I am creating my own physics for a character in a 2D side-scroller and one of the mechanics I want to have is 'Short Hops'.
Whilst looking around everyone uses the idea of GetKeyDown and GetKeyUp and using timers etc. However using GetKeyDown rather than GetKey, the jumping doesn't work anymore. I think it might have something to do with my use of grounded/air states but I am unsure.
Using GetKeyDown the character jumps about 2 cm above the ground and falls rapidly, however using GetKey the character jumps as expected using the formula:
public float timeToApex = 0.4f;
public float jumpHeight = 4;
gravity = -(2 * jumpHeight) / Mathf.Pow(timeToApex, 2);
jumpVelocity = Mathf.Abs(gravity) * timeToApex;
Here is a simple version of my code:
if (grounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
VelocityY = jumpVelocity;
}
}
Thanks in advance, Matt
Answer by dkjunior · Oct 19, 2015 at 05:13 AM
The difference between GetKeyDown and GetKey is that the GetKeyDown is triggered only once on the first frame the key was pressed while GetKey will be triggered on every frame while the key is pressed. So I'd guess the difference in observed behavior is due to the fact that even if you quickly press/release the space button GetKey version manages to get several VelocityY updates in, while GetKeyDown only sets it to jumpVelocity once and then lets gravity kill it.
Intuitively, GetKeyDown appears to be a better choice in your situation. You just need to tweak your jumpVelocity and gravity to make the jump appear as you want (it seems that you need to increase the jump velocity and decrease gravity).
Hi dkjunior, perhaps there is a problem within the script that is conflicting with this jumping method - in an old script I used this exact same mechanic and it worked; however I am rewriting the script to improve it. Would it be possible to run through the script? $$anonymous$$ainly split into two sections, a grounded state and an air state. http://pastebin.com/ze4zfrFk
Glanced over the code briefly. What is the purpose of the Space $$anonymous$$eyUp section below?
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))
{
if (VelocityY > $$anonymous$$JumpVelocity)
{
VelocityY = $$anonymous$$JumpVelocity;
}
}
This is something that may be causing the abrupt fall.
The code is just an attempt at short hops, having tried multiple different methods that was just the last one I've tried. However commenting out the Get$$anonymous$$eyUp statement doesn't change anything when pressing space.
Still leaving it as Get$$anonymous$$ey rather than Get$$anonymous$$eyDown works perfectly and as such am very confused to why this is.
I will continue to debug the code until I find what part is conflicting to cause this however if you have any more suggestions that would be fantastic
Your answer
Follow this Question
Related Questions
Regularly rotation control with keys,Regularly Rotation control by keys 0 Answers
need help with in-air movement 0 Answers
need help with jumping script 1 Answer
Jump logic issues 0 Answers
Player looses ability to jump further right player moves. 1 Answer