0
\$\begingroup\$

I'm making a 2d game, all is working, I have the characters and the map. The player have 6 different weapons and now I'm trying to make the collisions and the life / destruction system.

I have a character that follows the player. I made two triggers for this character and one for the player. When the player approaches, begins to haunt him, and when are near this character stop walking. To do this I programmed a "OntriggerEnter2d" and all is good, but now, I need to do anything similar with the bullets (or not). I tried OntriggerEnter2d and not works as expected, this detects the enter on the first zone of the bigger trigger, and I only need to detect the collision with the "body" of the characters.

Well, the question is, what is the best way to do the collisions between bullets and characters / objects??

Sorry if I explained bad the situation.

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

You can probably solve this using tag comparison. Tag the player as Player and inside your bullet code,

void OnTriggerEnter2D(Collider2D other) {
    if (!other.CompareTag("Player")) return;

    //Your code here
}

This makes it that the bullet ignore any trigger that is not tagged as the player.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Using physics layers might be better for performance - then the physics engine can be configured to completely skip the collision, not even processing it and calling your script in the first place. \$\endgroup\$
    – DMGregory
    Commented Oct 28, 2016 at 16:49
  • \$\begingroup\$ @DMGregory it would be better for performance but can turn out to be very messy for complex stuff. I ended up having layers for playerBullet, player, playerTargeting, enemyBullet, enemy and enemyTargeting in my last project. \$\endgroup\$
    – DarkDestry
    Commented Oct 28, 2016 at 17:07
  • \$\begingroup\$ Mmm...seems good, but I think this solution not works. The problem is that I have 2 triggers, one "internal" and other "external" on the same object, and, to detect the collision only on the "internal" one I don't know howto make it...unity don't have the tag option on sphere collider component....probably a different tag on the two triggers solve the problem, but is not the case. \$\endgroup\$
    – Jordi4321
    Commented Oct 29, 2016 at 7:23
  • \$\begingroup\$ I thinking that one possibility is check the colission with the bullet and the rigidbody of the objects, but I don't know how to do it, all I tried not worked \$\endgroup\$
    – Jordi4321
    Commented Oct 29, 2016 at 7:33
  • \$\begingroup\$ Well, i saw the solution...using another type of collider at the "internal" trigger of the opponent. Then, just I can use an if-type-do action, but, the bullet don't detect the "opponent character" trigger, only detect the player tigger. Bullet is a child object of the player. How I can solve this now?? How to detect collision with bullet and the collider of the another object?? \$\endgroup\$
    – Jordi4321
    Commented Oct 29, 2016 at 16:29
0
\$\begingroup\$

Well, finally I found the solution (thank you DarkDestry!). The mistake was assign the bullets to the player.

Now, I have the bullets separated from the character and I used layers to separate the colisions for the characters and the bullets.

Working perfect now.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .