- Home /
How to jump higher when you hold down with *touch* controls
So I looked through similar questions here on how to jump higher when a button is pressed but since my game is for mobile I need to make it based on when the user taps on the screen and holds it down for a certain period of time.
Here's what I have:
void Update () {
// Reset total jumps allowed when not performing a jump and grounded or when on a moving platform.
if (!jump && (player.grounded || player.IsStuckToPlatform())) {
jumps = doubleJumping.totalJumps;
}
//physical button jump
if (Input.GetButtonDown("Jump")){ //add touch to hold down here?
StartJump();
}
//tap jump
int nbTouches = Input.touchCount;
if(nbTouches > 0)
StartJump();
}
Void StartJump has the code for the jumping but the actual jumping based on how long a button was pressed is in fixedupdate. If I hold down up or spacebar my character does jump higher but when I tap on the screen the jump is the lowest jump possible as it doesn't detect that I'm constantly holding it down for a higher jump.
Edit: I think I know whats happening, its detecting my touch as a mouse click but doesn't detect when I hold it longer as I would a button. I can't find anything regarding holding down the touchscreen. OnMouseButtonDown doesn't work as it for some weird reason gives me an error.
Answer by tanoshimi · Mar 01, 2015 at 06:43 PM
You need to examine the touchPhase of each touch on the screen: http://docs.unity3d.com/ScriptReference/TouchPhase.html
In the frame in which the touch is first registered, this will be "Began". In every subsequent frame the touch is held, it will either be "Stationary " or "Moved", until it is ended or cancelled. So you can consider those two states as equivalent to a mouse or key button being held.
Answer by DedValve · Mar 01, 2015 at 09:32 PM
I tried that but I can't wrap my head around how I'm suppose to put it in this code. This isn't code I wrote btw, I'm trying to build on top of it and make mobile touch controls.
This is what I have in update:
//physical button jump
if (Input.GetButtonDown("Jump")){ //add touch to hold down here?
StartJump();
}
if (Input.GetTouch(0).phase == TouchPhase.Began){
StartJump();
}
This is Fixed Update which runs how high the player goes based on how long the button was pressed:
// If you need to hold the Jump input to jump higher...
if (jumpType == JumpType.HoldToJumpHigher) {
// When there is an initial jump...
if (initialJump) {
// ... set the y velocity to the player's initial jump value.
float yVel = jumpFactor * (doubleJump ? holdToJumpHigher.initialDoubleJump : holdToJumpHigher.initialJump);
And this is the start jump function:
void StartJump(){
// If the jump button is pressed, jumps are allowed and the player is not dashing, sliding, on a ladder or crouching under an obstacle...
if (!jump && jumps > 0 && !player.dashing && !player.sliding && !player.onLadder && (!player.crouching || (player.crouching && player.AllowedToStandUp()))) {
// If the player is grounded...
if (player.grounded) {
// ... initialize jump.
InitJump();
// If the player is not grounded and totalJumps is higher than 1...
} else if(doubleJumping.totalJumps > 1) {
// ... initialize jump if the Y velocity is inside the double jump window (or when there isn't a window).
if (!doubleJumping.jumpWindow || (doubleJumping.jumpWindow && rigidbody2D.velocity.y > doubleJumping.jumpWindowMin && rigidbody2D.velocity.y < doubleJumping.jumpWindowMax)) {
doubleJump = true;
InitJump();
}
}
}
}
I think what I have to do is figure out how to make the fixedupdate function into its own seperate function then in update call "if touchphase.stationary call holdtojumphigher();"
Problem is that the variable created in that fixedupdate is used in the rest of fixed update.