Forum begins after the advertisement:
-
::Reposting @l1nked712‘s suggestion:
In my opinion a simpler way to implement jumping is:
[Header("Jump")]
[SerializeField] private float jumpForce = 20;
private bool isGrounded = true;
void Start()
{
...
}
void Update()
{
...
Jump();
}
private void OnCollisionEnter2D(Collision2D collision)
{
isGrounded = true;
}
private void Jump()
{
if(Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
isGrounded = false;
}
}
Of course you could add extra constraints to the jump functions if such as if its actually colliding with the ground by adding a tag and then using gameObject.CompareTag but just for the square on this simple floor this would in my opinion be a much simpler implementation. Feel free to correct me if im wrong or if something is suboptimal in this solution.