Hello, beginner here so i face this issue. When player is jumping he falls down like in a picture. how to prevent this? it's rigidbody2d
`public class Player : MonoBehaviour
{
private float moveForce = 10f;
private float jumpForce = 11f;
private float movementX;
private Rigidbody2D myBody;
private Animator anim;
private SpriteRenderer sr;
private string WALK_ANIMATION = "Walk";
private bool isGrounded;
private string GROUND_TAG = "Ground";
private void Awake()
{
myBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();
}
void Start() { }
void Update()
{
PlayerMovementKeyboard();
AnimatePlayer();
PlayerJump();
}
void PlayerMovementKeyboard()
{
movementX = Input.GetAxisRaw("Horizontal");
transform.position += moveForce * Time.deltaTime * new Vector3(movementX, 0f, 0f);
}
void AnimatePlayer()
{
if (movementX > 0)
{
anim.SetBool(WALK_ANIMATION, true);
sr.flipX = false;
}
else if (movementX < 0)
{
anim.SetBool(WALK_ANIMATION, true);
sr.flipX = true;
}
else
{
anim.SetBool(WALK_ANIMATION, false);
}
}
void PlayerJump()
{
/*
adding force to rigidbody using vector to tell player to jump in 2D
ForceMode2D.Impulse tells it to use force right away
*/
if (Input.GetButtonDown("Jump") && isGrounded)
{
isGrounded = false;
myBody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag(GROUND_TAG))
{
isGrounded = true;
}
}
}`