D
D
Dark80902020-07-22 21:19:15
Unity
Dark8090, 2020-07-22 21:19:15

[Unity2D] How to fix [script]jump while taking damage?

The problem is that when receiving damage (any) and pressing the spacebar (jump) during the damage received, the character jumps very high, but it is necessary that when receiving damage, the character only repels and it is not possible to use the jump when the character is damaged.

Jump method:

private void Jump()
    {
        rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
    }

The method of taking damage (and pushing the character away when taking damage):

public override void ReceiveDamage()
    {
        Lives--;

        rigidbody.velocity = Vector3.zero;
        rigidbody.AddForce(transform.up * 6.0F, ForceMode2D.Impulse);

        Debug.Log(lives);
    }


Method to check for isGrounded (whether the person is on the ground before jumping)

private void CheckGround()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
 
        isGrounded = colliders.Length > 1;
 
        if (!isGrounded) State = CharState.Jump;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-07-23
@zZaKko

Add another bool variable and when taking damage make it true, and in the jump method check it for false. And when colliding with the ground back, make it false.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question