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
0
Question by DJJD · Feb 12, 2018 at 04:49 AM · collidertriggertransform.positionboxcollidercenter

Check if the center of an object is inside a trigger Collider?

Hi guys!

I would like to know if there is a way to check if the center of an object is inside a Trigger Collider.

I have a simili Weather System, where the temperature varies in different Weather Zones. These zones are trigger Colliders.

I want to be able to tell in which zone an Island Is, based on its center, because I don't want to have an Island included in different zones. Is there a way to check that? Or do I have to put a child empty object with a small collider in the middle of the parent object and check by that collider?

Thanks a lot!

Comment
Add comment · Show 2
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 raoul_95 · Feb 12, 2018 at 05:18 AM 0
Share

Compare world positions if its equal its in center of collider

avatar image DJJD · Feb 12, 2018 at 01:41 PM 0
Share

I don't want to see if the center of my object is at the center of the trigger, I want to see if the center of ly object is somewhere inside the trigger. If the center is close to the left border, I want to tell that it is inside the same way it would be at the right Border. It's juste to be sure that a single object is not inside 2 triggers next to each others at the same time.

1 Reply

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

Answer by yummy81 · Feb 12, 2018 at 03:49 PM

When it comes to 3D, I would use Physics.OverlapSphere. You can supply it with two parameters: position and radius. But the most important is that if you set the radius to 0.0f, you will get the point. And that's want you want. So, when the method detects that it is inside the collider (or colliders), it will simply return the array of colliders. At that moment, you only have to get the collider you are interested in from this array, but in your case the simplest solution would be to check only for the first element. I wrote the code for you. Just attach the script to your Island gameobject. The Island does not have to have Rigidbody or any sort of collider for Physics.OverlapSphere to work, but gameobjects against which you want to perform a check (weather zones) must have their own colliders. Here's the code:

     public Collider[] colliders;
     
     private void Update()
     {
         colliders = Physics.OverlapSphere(transform.position, 0.0f);
         if (colliders.Length > 0)
         {
             Debug.Log(colliders[0].name);
         }
         else
         {
             Debug.Log("I'm outside the weather zone");
         }
     }
 
 

The same goes for 2D. Just change:

 public Collider[] colliders;
 
 

to this:

 public Collider2D[] colliders;
 
 

and replace this:

 colliders = Physics.OverlapSphere(transform.position, 0.0f);
 
 

with this:

 colliders = Physics2D.OverlapCircleAll(transform.position, 0.0f);
 
 

Still for 2D, you can also use Physics2D.OverlapCircle, which returns just one collider (the one you want) without all this mess with arrays. And there is Collider2D.OverlapPoint, which implementation requires delegates in your case.

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 DJJD · Feb 12, 2018 at 09:39 PM 0
Share

Very interesting! It is in 3D indeed, and your answer seems to make sense. I've never used OverlapSphere before, so I'll check this out after university tonight to be sure it fits what I want, but thanks in advance!

avatar image DJJD · Feb 16, 2018 at 12:44 AM 0
Share

Did not have much time to test it recently, but I finally just did, and it works perfectly. Thanks a lot, I'll add the OverlapSphere (and circle for 2D) to the usefull things I know.

Thanks!!

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

104 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

Related Questions

Move sprite depending on its collider position 1 Answer

why isn't my OnTriggerEnter2D() function working? 14 Answers

How to check if a BoxCollider is hit by a trigger? 1 Answer

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

Collider2D trigger still acts like a collider 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