- Home /
The question is answered, right answer was accepted
Mobile platformer jump not working 2D
Hey, I`m new to mobile inputs and can`t figure out how to make my player object jump. I found lots of answers, but none of them worked.
I need my player object to jump on a constant height/with constant force, when I tap anywhere on the screen ONCE, not on a specific button.
One of the solutions worked
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if(Input.touchCount > 0) {
rb.AddForce(transform.up, ForceMode2D.Impulse);
}
}
But it adds too much force depending on how hard I tap the screen, so that the character can fly far away. I`d like to make it jump on a fixed height or applying fixed force value, so that it doesn`t fly away. Other ways of applying force to the object don`t work and I can`t tell why. Same goes for velocity.
I would appreciate any help, thanks.
Answer by NoseKills · Aug 21, 2017 at 05:35 PM
Add the impulse only when the touch starts, not every Update() when there's a finger on screen.
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began) {
rb.AddForce(transform.up, ForceMode2D.Impulse);
}