Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
1
Question by R1PFake · Nov 21, 2016 at 06:50 PM · physicsrigidbodycollidertriggertriggers

Is it possible to combine child trigger of a object to only trigger OnTriggerEnter once?

Hello, i want to hit monsters with a sword, the sword has a collider and i use the OnTriggerEnter to deal damage, my monsters have different shapes and a default collider/trigger doesn't fiit that's why i use multiple trigger on the monsters to fill the monster shape.

But the problem is, because there a multiple triggers my sword OnTriggerEnter sometimes triggers twice in the same "swing". For example let's say a human like enemy which has a trigger on the head and body, if my sword would swing from the top to bottom it would trigger the had and body and get the OnTriggerEnter call twice.

Is there a way to combine the monster child triggers into a single trigger so that my sword slash in the example above would only trigger when i hit the enemy head and call trigger enter when i leave the body trigger?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by oStaiko · Nov 21, 2016 at 07:35 PM

Well, complex triggers have their disadvantages of course. There is no "easy" way to do this.

I'm going to guess a few things about your game. The player isn't constantly attacking, and has to do something to initiate an attack. An enemy should only be hit once per attack by the sword, and there's no spinning attacks that would hit them twice per swing. Lastly, all damageable entities descend from the same class inheritance.

If those assumptions are true, a simple thing to do would be whenever your sword hit an enemy, to deactivate its collision box after applying damage. This would prevent hitting an enemy twice with one swing. You could then reactivate the collision detection when you initiate a new swing.

But then lets say there's 3 enemies in a row, and you want to be able to hit all of them with one swing. If you deactivate the sword collision on the first one, it'll simply pass through the others, so that wont do here!

For every swing of a sword, you can store a reference to each entity hit. If you are using instances of an enemy script or such, you can do something like:

 using System.Generic.Collections;
 
 List<EnemyScript> hits;
 
 void Swing ()
 {
     hits = new List<EnemyScript> ();
     //Do your swing stuff here
 }
 
 void OnTriggerEnter (Collider other)
 {
     EnemyScript script = other.GetComponent<EnemyScript>();
     //Depending on your set up, getting the reference may need to be done differently 
     if (script != null && hits.Exists(script)))
     {
         hits.Add(script);
         //ApplyDamage and such here
     }
 }

This prevent the same instance of an enemy from being hit twice by any collider with that script, per Swing(). I didn't test it, but it should work.

If you have different types of enemies, with different scripts, this can be done with something called inheritance. There's a nice Unity guide Here that covers it, just make a new class "Damgeable" that has an "OnDamage" function, and make every damageable entity inherit the Damageable class. You can then call OnDamage(params) in the script above, easy as that! Any other questions on inheritcance should be answerd by the video.

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 R1PFake · Nov 22, 2016 at 05:40 PM 0
Share

Hello, thanks for the long answer, i thought that maybe there is a "simple" way to combine the triggers without extra code in the Trigger $$anonymous$$ethods but i guess there is not? $$anonymous$$y collider objects have a "HitBox" Script which has a reference to the parent character/enemy, I have a Interface IRpgCharacter for them.

$$anonymous$$y triggers are big enough so that they always overlap a little.

I expanded your example a little for my needs, i made a Dictionary which has a IRpgCharacter as $$anonymous$$ey and a list of HitBox (the colliders) as value. Now OnTriggerEnter i check if there is already a key for the character, if there is it means that im already inside a trigger of this character and i don't deal damage and just add the collider to the list, otherwise i create a new dictionary entry and also add the collider to the list of the new character entry

OnTriggerExit i check if there is a character in the dictionary for the hitbox, if there is not, then i don't do anything (this should never happen in theory, because OnTriggerEnter would be called first and should add a entry for the character) If there is a character then i remove the current hitbox collider from the list and check the list count, if there list is empty it means that i left the last trigger of the character and i remove the character from the dictionary so that the next "new" OnTriggerEnter can deal damage again.

This soltuion works good so far for my project but its important that this only works because my trigger are big enough to overlap, if they wouldn't overlap then it wouldn't work

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.

Follow this Question

Answers Answers and Comments

109 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger doesn't work 100% of the time. 1 Answer

Rigidbody.SweepTest Hit Trigger Colliders 0 Answers

OnTriggerEnter problems 3 Answers

Simple Triggers problem. 1 Answer

Physics Possibilities?: Angles, Penetration, Exceptions 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges