Problem with swipe and player jump
Good night, all right? I'm programming a mobile game and I'm the problem: I have a code to check if the player makes the "swipe" on the screen up to perform a leap of character. The code works correctly, however, the size of the leap varies according to swipe the size of the player did, how can I adjust so that, regardless of size, the jump is the same height (I set the AddForce)?
if(Input.GetTouch(0).phase == TouchPhase.Moved) {
var posicaoDeltaTouch : Vector2 = Input.GetTouch(0).deltaPosition;
if(posicaoDeltaTouch.y > 5 && posicaoDeltaTouch.x < 5 && posicaoDeltaTouch.x > -5 && jogador.GetComponent(ColisaoPlayer).naSuperficie == true) {
jogador.GetComponent(Rigidbody).AddForce(0, 400, 0);
}
}
Thank you!
Sorry, I can't understand, is your jump now depends on swipe size or not? it seems not, because i in AddForce(0, 400, 0)
400 is fixed number
Currently the jump is depending on the swipe size also can not understand why, since they define a specific value. The jump is not to depend on the size swipe, in case if the player makes a swipe greater than the set, the jump must be the same size.
Answer by Rostam24 · Sep 29, 2015 at 10:13 AM
It's probably being called several times. Place a debug.log() right where you make the character jump to check if this is indeed happening. If so, you'll have to add another check so that players can't jump twice with 1 swipe.
@niick555: checking if the character on the ground might not be good enough, as a quick / big swipe might cause several 'jumps' before the character is in the air. Test if this is happening by debugging as I mentioned earlier. And if so, you'll need an extra check. For instance, a bool 'firstJump' that is set to false after jumping, and is set to true when touchphase is began.
I auditioned and is really going on several jumps. Tried to make firstJump variable as you said, but I believe I did wrong, why did not solve the problem ... Can you help? All of the code below is within the Update (). The variable declaration is outside the Update ().
if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
var posicaoDeltaTouch : Vector2 = Input.GetTouch(0).deltaPosition;
firstJump = true;
if(posicaoDeltaTouch.y > 5 && posicaoDeltaTouch.x < 5 && posicaoDeltaTouch.x > -5 && jogador.GetComponent(ColisaoPlayer).naSuperficie == true && firstJump == true) {
jogador.GetComponent(Rigidbody).AddForce(0, 400, 0);
primeiroPulo = false;
Debug.Log("Jump");
}
}
Thank you!
Give this a try: if(Input.GetTouch(0).phase == TouchPhase.Began) firstJump = true;
if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
var posicaoDeltaTouch : Vector2 = Input.GetTouch(0).deltaPosition;
if(posicaoDeltaTouch.y > 5 && posicaoDeltaTouch.x < 5 && posicaoDeltaTouch.x > -5 && jogador.GetComponent(Rigidbody).naSuperficie && firstJump) {
jogador.GetComponent(Rigidbody).AddForce(0, 400, 0);
firstJump = false;
}
}
The code looks a bit weird, as not all of it is in the same language - but I'm sure you can make the necessary corrections to make it work :)
Answer by niick555 · Sep 29, 2015 at 11:19 AM
@Rostam24 I understand, but it turns out that there is already a variable "naSuperficie" that makes the player jump only when it is on the ground, that is, you can not make another jump while it is in the air. I do not understand what may be happening ...
Your answer
Follow this Question
Related Questions
How to optimize touch input script 0 Answers
Jump by touch Unity C# 0 Answers
How to replace KeyCode.Space with a simple tap on Android? 1 Answer