I am making a melee attack with my game, but I'm stuck in a problem related to using triggers. The enemy NPCs have a sphere collider set on trigger so I will know who is inside their range (radius of 15).

Then, I wrote a simple OnTriggerEnter script to make that when my sword (its collider is set to trigger as well, so it will pass through the enemy) hits an enemy NPC, it will destroy that enemy. The problem is that when I test it, the enemies are destroyed once I enter their sphere collider.

So, is there a way to ignore the trigger from their shpere collider? Or what is the best way to do a melee attack?

NOTE: the destroy function is public in the enemy script and it was called in weapon script attached to the weapon.

  • \$\begingroup\$ You are thinking too hard. Why would you keep a list of who is inside a range? Just check who hitted him before dying. \$\endgroup\$ – S. Tarık Çetin Dec 5 '15 at 12:55
  • \$\begingroup\$ So, what you are saying is that the problem is when your main characters enters the trigger area of the enemy, the enemy already dies even if the sword did not touch the enemy? \$\endgroup\$ – MAnd Dec 5 '15 at 20:39
  • \$\begingroup\$ @MAnd yeah. it is. \$\endgroup\$ – user3717030 Dec 6 '15 at 0:12
  • \$\begingroup\$ @SamedTarıkÇETİN the reason i need sphere collider is to know who will the ai chase, my game is like a zombie infection, there are a human ai and zombie ai, and the zombie ai will chase the player/human ai inside the sphere collider and give up if it exit the shpere collider \$\endgroup\$ – user3717030 Dec 6 '15 at 0:14
  • \$\begingroup\$ Then please, share the piece of code you are using for that. Then we could be able to help you further by adapting the solution to your specific case. \$\endgroup\$ – MAnd Dec 6 '15 at 0:28

Not sure if I understand the question correctly, but let's try. Add an empty child to your hero character and add the sphere collider to this empty object.Add this object to a layer of it's own , e.g "radar". Let's say your enemy are assigned layer "enemy". Now, go to Edit->Project Settings->Physics and, set the matrix so that the "radar" does not collide with any layer other than "enemy".

Now, radar will have a collision detection script that will return the object with which it collided. The hero script will call this method to check if radar collided with anything and if it did, what did it collide with.

  • \$\begingroup\$ thanks. it not the answer im looking for. but i got the idea from here. thanks \$\endgroup\$ – user3717030 Dec 5 '15 at 12:00

Well, it is most probably because you have your Trigger (Sphere) collider attached to the same object where your Sword would work. So it is showing the same behaviour as sword hits the enemy, because of same object and script.

What you can do is ,

  • Make a parent gameobject in which player and another gameobject should exist as children.
  • Attach two scripts separately on both (sword and sphere).
  • Move parent as first person.

In this way you can get different trigger events for sword and sphere.

Hope this helps.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.