• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by brenolimaa96 · Apr 26, 2016 at 10:30 PM · gameobjecttransformontriggerenteroncollisionenterboxcollider2d

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image brenolimaa96 · Apr 27, 2016 at 04:30 PM 0
Share

@Thajocoth,

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 determines 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.
  
  }



avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pankajdeepsahota · Jul 03, 2020 at 10:41 AM 0
Share

I am having the same problem. Did you find any solution ? Please reply .... Thank you !!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i assign a GameObject to another but only have some of its components? 1 Answer

GameObject not looking at me.. 1 Answer

Enemy Attacking Even When Not In Range 2 Answers

Change the Rotation X value without changing Y and Z 1 Answer

Add Force isnt working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges