I have personally always done a
if(onGround && tryToJump)
jump();
jump(){
yVelocity = 1000;
}
where jump was an instant upward velocity, lets say for example +1000 and gravity was a constant -5 per tick. for 3 seconds of airtime approximately?
However, in my opinion, my solution(at least in my mind, without testing it)
lets say you want jump held for 1 full second is full height jump
just set a counter in milliseconds or whatever start when you press jump, and when you release it
so if you release after 500ms take 500 off that initial 1000 upward,
or if you held for 300 ms, take off 700 from that 1000.
and obviously if you held for over 1000ms, then nothing would happen
jump(){
yVelocity = 1000;
counter(); // start timer
}
onJumpButtonRelease(){
if(counter.elapsed<1000)){
yVelocity = yVelocity - counter.elapsed;
}
}
This would obviously need to be balanced/tested against gravity, to make sure it doesn’t cause a sudden downward movement, but simply a reduction in total height reached.