Killing enemy jumping on his head
Hello everyone, My 2D game is almost like Mario, and I’m having some problems to kill the enemies jumping on its head.
My enemy object has a collider2D with a onEnterCollision2D that do some damage to my player.
however my enemy object has a children, that is the head object. I’ve did this head object as a children so that way it could “follow” the body (enemy object).
My head object has a collider2D with a onEnterCollision2D that the player can destroy the enemy object jumping on it.
The problem is: When i jump in the head i can kill the enemy, but stills lose health.
I thought that it can be a conflict about the 2 colliders, thinking that one is parent from the other.
Idea of solutions:
Stick somehow the head object with the collider in the enemy object with the collider, without doing parenting.
A way to avoid the children from getting the parents components.
Any Idea how should i solve my problem?
Answer by Thajocoth · Apr 27, 2016 at 12:13 AM
An object uses all of the colliders that it & its children have. You're probably entering both sets of code. I would recommend this: Instead of having two colliders, in your collision function check which object's center is higher (with an offset if needed), and either kill the enemy or damage the player based on that.
Alternatively, you could have a trigger collider (Collider2D with IsTrigger checked) in the player's shoes and enemy's head and a non-trigger collider in the enemy's shoes and player's head, with some empty space between them. As long as the empty space is not big enough for the sprites to pass through each other, it should be fine. In this instance, you would be damaging the player from OnCollisionEnter2D() and damaging the enemy from OnTriggerEnter2D(). The only downside here is that you'd have to add the bounce force manually after damaging the enemy.
In neither of these solutions does it help you to have the enemy's head be a child of the enemy... But it doesn't hurt you either, so do as you will.
I've spent all the morning to solve this problem, i've tried messing up with the offset and now i'm completely without a logic that i can use, please someone help me with a logic that i need. I think that i have to use some bool in the OnCollisionEnter2D.
I'll show my code about killing the enemy.
public class $$anonymous$$illEnemy : $$anonymous$$onoBehaviour {
private Animator anim;
public GameObject inimigo;
private bool died= false;
public GameObject player;
private Rigidbody2D RbPlayer;
private bool positionHead= false;
private bool positionBody = false;
private BoxCollider2D boxEnemy;
private Dano damage;
void Start () {
anim = GetComponent<Animator>();
RbPlayer = player.GetComponent<Rigidbody2D>();
boxEnemy = GetComponent<BoxCollider2D>();
damage = GetComponent<Dano>();
}
void FixedUpdate () {
if (died == true)
{
anim.SetBool("died", died);
GameObject.Destroy(enemy);
}
}
void OnCollisionEnter2D(Collision2D colisor)
{
if (colisor.gameObject.name.Equals("Player") )
{
damage.Attack(); // the damage when player touchs enemy
}
if (colisor.gameObject.name.Equals("Player") )
{
RbPlayer.AddForce (new Vector2 (0, 150));
died= true; // player gets an up force when kill enemies
}
}
void setBody(){
boxEnemy.offset = new Vector2 (boxEnemy.offset.x, -0.06f);
positionBody= true;
}
void setHead(){
boxEnemy.offset = new Vector2 (boxEnemy.offset.x, -0.0025f);
positionHead= true;
}
// those last functions deter$$anonymous$$es the position of the offset,
//the setBody puts the collider only in the body and the setHead put
//the collider in head and body.
// please help me to find some idea how should i do, I have to put some //logic in the both ifs of the OnCollisionEnter but dont know how to do.
}
Answer by boutgamer1 · Apr 29, 2020 at 09:20 PM
Hello, I have the same problem..
In my game2D, I have a Enemy with his collider (body of enemy) + his child GameObject with collider (head of enemy).
Here is a part of Enemy (body) script :
public void OnCollisionEnter2D(Collision2D collision)
{
if (col.transform.CompareTag("Player"))
{
//Si le joueur (player), saute sur la tête du boss,
//ce dernier perd de la vie (-20) sur sa barre de vie.
BossHealth bossHealth = boss.transform.GetComponent<BossHealth>();
bossHealth.TakeDamage(damageOnCollision);
}
}
Here is a part of his child (head of enemy) script :
private void OnCollisionEnter2D(Collision2D col)
{
if (col.transform.CompareTag("Player"))
{
//Si le boss touche le joueur (player), ce dernier perd de la vie
//(-20) sur sa barre de vie d'un total de (100).
PlayerHealth playerHealth = col.transform.GetComponent<PlayerHealth>();
playerHealth.TakeDamage(damageOnCollision);
}
}
But it doesn't work! When the player collied with the (head of enemy collider), the player loose health !!!, like if he collid with the parent GameObject (the body of the enemy)... of course for testing, I increased the size of the enemy head collider, like this I'm sur that I collied with the head collider and not the body collider of enemy.
Did you found any solution for this problem?
Thanks in advance.
I am having the same problem. Did you find any solution ? Please reply .... Thank you !!