Question by
richa0305 · Jan 08, 2019 at 02:57 PM ·
unity 2dvelocityjumping object
Jump at fixed height on single touch : Unity 2D
I am trying to make replica of stack jump game for learning. I am trying to make my player jump for fixed height but with below code it always jumps at different height. If i touch for long then it jumps high and if i touch and release release immediately then it jumps very low. I want my player to jump constant height for long touch or for short touch both. I have just started learning unity. Please help!
Here is my code in FixedUpdate() method -
Touch touch;
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if ((touch.phase == TouchPhase.Began) && isGrounded)
{
//rb.velocity = Vector2.up * jumpForce;
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
isGrounded = false;
}
if (touch.phase == TouchPhase.Ended)
{
//rb.velocity = Vector2.down * jumpForce;
rb.AddForce(Vector2.down * jumpForce, ForceMode2D.Impulse);
isGrounded = true;
}
}
Comment