void Knockback(Collision2D collision) { Knockbackable k = collision.gameObject.GetComponent <Knockbackable>(); if (k) { for (int i = 0; i < collision.contacts.Length; i++) { k.RB2D.AddForceAtPosition((-collision.contacts[i].normal) * (k.KnockbackForce * 1f / collision.contacts.Length), collision.contacts[i].point, ForceMode2D.Impulse); } } }
void OnTriggerEnter2D(Collider2D collider) { Damageable dmg = collider.gameObject.GetComponent <Damageable>(); Knockbackable kb = collider.gameObject.GetComponent <Knockbackable>(); if (dmg != null) { dmg.doDamage(emitter, damage); } if (kb != null && rb.velocity != Vector2.zero) { Vector2 force = (collider.transform.position - transform.position).normalized * knockbackIntensity; kb.knockback(force, knockbackDuration); } }
void OnTriggerEnter2D(Collider2D collider) { GameObject other = collider.gameObject; Damageable dmg = other.gameObject.GetComponent <Damageable>(); Knockbackable kb = other.gameObject.GetComponent <Knockbackable>(); if (dmg) { dmg.doDamage(emitter, damage); } if (kb && knockbackForce != 0) { Vector2 force = (collider.transform.position - transform.position).normalized * knockbackForce; kb.knockback(force, knockbackDuration); } }
private void OnTriggerEnter(Collider c) { if (c.tag == "Enemy") { Knockbackable kb = c.GetComponent <Knockbackable>(); if (!kb) { Debug.LogError("No kb script found attacked to" + c.name); //Do nothing if there is no knockback script(not knockable) } else { if (!kb.isknockback)//check if it is currently being knocked back, if not continue. { kb.ApplyKnockBack(kbpoint, knockbackforce); } } } }
private void DoKnockback(Collider2D other, Vector2 point) { Knockbackable knockbackComponent = other.attachedRigidbody.GetComponent <Knockbackable>(); if (knockbackComponent != null) { Vector2 direction; if (overrideKnockbackDirection) { Vector2 scale = transform.lossyScale; direction = knockbackDirection * new Vector2(Mathf.Sign(scale.x), Mathf.Sign(scale.y)); } else { direction = (Vector2)other.transform.position - point; } knockbackComponent.Knockback(direction.normalized, knockbackForce, knockbackDuration); } }