• 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
Question by cinan032 · Aug 07, 2016 at 01:21 PM · physicscolliderphysics2dcollider2dnewbie

Using collider to gameobject detect attack or not

I am using collider to detect the monster in player attack area or not.

And also, to detect the player in monster attack area or not.

But the problem is that will also triggering the monster "OnTrigerEnter2D" when monster going in player collider area. (player is not in monster collider area)

it mean the trigger will be triggered by other gameobject collider?

How can i fix that? Or i should not use collider to do that?

alt text

triggerproblem.jpeg (19.2 kB)
Comment
Justinger

People who like this

1 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by cinan032 · Aug 09, 2016 at 03:19 PM

I already fix the problem.

The first point is as @Cence99 said, I need to use OnCollisionEnter2D instead of using OnTriggerEnter2D. Thanks a lot, @Cence99.


And, I changed the structure of the Player and Monster GameObject:

Player/Monster GameObject:

  • Body (RigidBody and Collider)

  • AttackArea (Kinematic RigidBody and Collider to detect monster target)


Last, using layer collision matrix to make both of the Collider in Body and AttackArea will not effecting the other one.

Comment

People who like this

0 Show 0 · 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

Answer by Cence99 · Aug 07, 2016 at 10:37 PM

If you really want to do that with colliders, you would need to go with 2 colliders per entity. So the player has a small collider which can be detected by the monster's big radius collider, and the monster has a small collider that can be detected by the player's big radius collider.

I would recommend using Vector3.Distance though and checking if the monster is in the player's attack range and vica versa.

Example::

 public class Player : MonoBehaviour
 {
     public Transform monster;
     public float attackRange = 3;
 
     void Update()
     {
         if(Mathf.Abs(Vector3.Distance(transform.position,    monster.position)) < attackRange)
         {
             // Monster is in attack range of player, do your stuff
         }
     }
 }
Comment
Justinger

People who like this

1 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 cinan032 · Aug 08, 2016 at 03:43 PM 0
Share

I use collider to detect monster because the game will have many monster.

So, i think it may use more resources than using collider if use "update" to loop to check all of the monster"s" in range or not.

avatar image Cence99 · Aug 08, 2016 at 03:50 PM 0
Share

I don't know about that, it could eventually be true. Create a child object for player and monster then which you call "AttackRadius". At this object you add a big radius trigger collider and a script that uses OnCollisionEnter to detect if player/monster is in attack range. On the player/monster itself, add a much smaller collider that is non-trigger.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Colliders2D not working under same parent? 2 Answers

Is it possible to rotate a 2d collider in 3d? 0 Answers

Making objects fall, that are no longer connected to the ceiling through other objects. 0 Answers

Creating 2D collider from 3D mesh 0 Answers

Making objects fall, that are no longer connected to the ceiling through other objects. 1 Answer


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