Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is there a possibility to identify the colliders ?

Discussion in 'Scripting' started by Louloulouliu, Feb 23, 2016.

  1. Louloulouliu

    Louloulouliu

    Joined:
    Feb 23, 2016
    Posts:
    3
    Hello,

    I'm new to unity and I have a question about colliders : how to identify the collider ?
    The game I try to create is a simple 2D game with 2 characters: my player and an enemy.
    My player has a hurtbox (boxcollider all around him), the enemy aswell. The difference is my player can make an attack with a sword in front of him.
    • If the player hurtbox enters in collision with the enemy boxcollider, I want my player to get hurt
    • If the player sword boxcollider enters in collision with the enemy boxcollider, I want the enemy to get hut.
    A possibility could be to have 1 GameObject (Player) and an other GameObject (Sword) inside, with its own collider. So in the OnTriggerEnter2D(Collider2D other) method, other.name should be Sword or Player, and I could identify if my Enemy collider is getting entered by a Sword or the Player (and thus damage the enemy or not).

    But is it possible to manage such collisions with having the whole player (him and his sword) being a single game object ? For example in tagging the different colliders inside the Player GameObject ?

    Thanks in advance for the answers !
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,738
    Make the sword it's own gameobject with its own collider. Than to tell things apart you can read the tag of other in the collision or do other.GetComponent to see if the colliding object has the right script on it.
     
  3. Louloulouliu

    Louloulouliu

    Joined:
    Feb 23, 2016
    Posts:
    3
    Thanks for the reply, I'll try this !
     
  4. Louloulouliu

    Louloulouliu

    Joined:
    Feb 23, 2016
    Posts:
    3
    Hi again,

    I have a problem with this : when the sword (which is now a child GameObject) collides with the enemy, the parent method OnTriggerEnter2D is called also. It means I can't know if the player got hurt or if the sword touched the enemy!