Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by W1k3 · Jul 17, 2014 at 07:19 PM · triggercolider

Using a colider to detect presence of a trigger (C#)

I'm going to refer to a colider not set as a trigger a "colider", and a colider set as a trigger a "trigger".

I have a shaped trigger attached to my player so that it can act as a sword swing area. when the enemy enters the trigger and you "swing your sword" I want the enemy to receive damage. unfortunately, with tags, the player cannot tell which enemy receives damage, and as far as I can tell, a colider cannot detect if it is being touched by a trigger.

Is it possible to to add another colider to my enemy as a trigger, and have it detect the players trigger? Is there a way for the specific enemy colider to tell when it's in the player 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
0

Answer by ajkolenc · Jul 18, 2014 at 12:43 AM

It should be quite possible to detect which enemy entered your sword-area trigger via the OnTriggerEnter function. If I understand your setup correctly, this is what I would recommend:

  1. Set up a list (or HashSet) of whatever type of script your enemies have attached. Alternatively, you could simply have a list of Transforms. This will serve as a record of all the enemies that are currently in attacking range.

  2. In your OnTriggerEnter function, check if the collider that your sword-area detected is an enemy, either using tags or checking for the presence of a script.

  3. If you did collide with an enemy, add the enemy to the list you made in step 1.

  4. In your OnTriggerExit function, check if the departing collider is in your list of enemies. If it is, remove it from the list.

  5. When you swing your sword, apply damage to all the enemies in your list.

I strongly recommend using a HashSet because of the efficiency of the Contains and Remove functions.

It should be noted that in order for your sword-area to detect the presence of an enemy, it will need to have a Rigidbody attached to it or one of its parents. Check this collision matrix to make sure that the colliders you are dealing with will send messages to one another.

Comment
Add comment · Show 2 · 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 W1k3 · Jul 18, 2014 at 04:37 AM 0
Share

The enemies are all clones. How can I tell which enemy to deal damage to if they are all the same? I did manage a script that gives them all different names, but when an enemy enters the trigger, how do I add that specific enemy?

avatar image ajkolenc · Jul 20, 2014 at 11:32 AM 0
Share

The OnTriggerEnter function takes in a Collider as a parameter, and from that collider you can find which specific object you hit, no names required.

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

22 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

Related Questions

Can't click gameobject when over another trigger? 1 Answer

Double Colider trigger 3 Answers

Play parent animation without affecting the child animation 1 Answer

Get a UserTrigger in Javascript - Eg for SmoothMoves 1 Answer

Make one trigger enter true for overlapping triggers 0 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