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 KyleLeitao · Sep 21, 2018 at 03:19 AM · collision2d gamecolliders2d-physicscollision2d

How to tell if a 2D object is in a certain area

I'm creating a game field and trying to figure out how to tell if a 2D object is on a certain side of the field. How can I go about doing this?,I'm creating a field and I am trying to figure out how to create a bool returned of some kind if the 2D object is on one side of the field. How can I go about doing this?

Comment
Add comment · Show 1
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 Atiyeh123 · Sep 21, 2018 at 06:52 AM 0
Share

hi, one way is to add a separate collider to each field

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by theInsomniacGameMaker · Sep 23, 2018 at 06:42 AM

This is what I understand from your question. You want to divide your game field into two parts and you want to know if a game object is in a certain section. I can think of two ways to approach this problem. One using the position of the game object and another using Trigger Colliders.

I think the Trigger Collider is the best way, it will save you a lot of lines of code.

Make two empty game object and add two BoxCollider2D components to both of the game objects. Name them "Section 1" and "Section 2". Also for the BoxCollider2D component, check isTrigger checkbox.

Now to the object that you want to detect the side of the field. Attach a script to it if it doesn't have a script attached to it. In the script add two bools.

     bool isInSection1, isInSection2;

After that add a new function called OnTriggerEnter2D. Below is how your logic would look like.

     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.name == "Section 1")
         {
             isInSection1 = true;
             isInSection2 = false;
         }
         else if (collision.gameObject.name=="Section 2")
         {
             isInSection1 = false;
             isInSection2 = true;
         }
     }

When the isInSection1 is true, it is in the first section and obviously the same holds true for the isInSection2.

Also, remember the object that you are detecting for should have a Rigidbody2D component and also some sort of 2D collider.

You can adjust the BoxCollider2D component of the empty game object to make the sections as big or as small you would like.

Read about the BoxCollider2D in the documentation and the OnTriggerEnter2D function in Scripting reference.

Comment
Add comment · 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
0

Answer by KyleLeitao · Sep 23, 2018 at 06:45 AM

I figured out the problem. It was the ignore raycast. I couldn't use the mouse because of it.

Comment
Add comment · 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

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

158 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 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

Why is my character not colliding with other objects. 3 Answers

Player getting stuck between grounds 0 Answers

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

Tilemap collider doesn't appear 1 Answer

How can I make sure triggers or collisions are never skipped even for high speeds? 2 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