In 2D platformer I am having issue with knockback characteristic that occurs when character touches enemy.
That is implementation of it.
Character.cs:
public IEnumerator KnockBack(float knockbackDuration, float knockbackPower, Remodel obj)
{
float timer = 0;
whereas (knockbackDuration > timer)
{
timer += Time.deltaTime;
Vector2 course = (obj.rework.place - rework.place).normalized;
rigidbody.AddForce(-direction * knockbackPower);
}
yield return 0;
}
Enemy.cs invokes that knockback:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Participant")
{
StartCoroutine(participant.KnockBack(knockbackDuration, knockbackPower, this.rework));
}
}
When character stays nonetheless, enemy approaches it and triggers regular knockback. With pressed motion key char comes towards enemy and simply pushes it. Nothing else occurs. And if it jumps towards enemy, knockback may be very, very highly effective.
I attempt to get all of it behave in the identical means. Enclosing code for character actions, simply in case. Perhaps vectors of rigidbody are overlapping someway and that is the difficulty.
non-public void Stroll()
{
float transfer = Enter.GetAxis("Horizontal");
rb.velocity = new Vector2(transfer * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
non-public void Soar()
{
if (Enter.GetKeyDown(KeyCode.House) & onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
Additionally, if it’s important, each of them have a BoxCollider2D.