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 Matthew_S · Aug 29, 2019 at 06:32 PM · collisionupdatecollision detectioncollider2d

Detecting whether or not 2 objects are colliding in update()

I am making a 2D tower defense game and am currently struggling with a problem with placing towers. Currently, I press a button to create a "phantom tower" which follows the mouse. I want to be able to click to place the tower, but I cannot figure out how to properly detect whether or not the tower is colliding with the road/water/obstacles. I have polygon colliders over the unplaceable areas of the map and a box collider on the phantom tower. I have an OnTriggerEnter which changes the placability to false and an OnTriggerExit which makes it placeable again. When I Debug.Log the placeability of the tower in update, it is inaccurate and wonky. When I Debug.Log in the collision detection, it gives the correct results. I need to be able to get the actual placeability in update so that I can detect the click to place the tower properly. I don't know much about the execution order, update/fixedupdate/physics stuff, but I'm pretty sure this is the problem Does anyone have a solution to detect the collision in update?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 30, 2019 at 10:19 AM

It's not clear what you actually do inside OnTriggerEnter / OnTriggerExit. Though if you have multple areas that are blocking the placement you get issues when your tower enters two of these regions at the same time. Because when you just use a single boolean value to remember the placability state you get a wrong value when you leave one of the two areas but you're still inside the other. You would need to remember the state for each individual collider


This is generally a tricky problem. There are several solutions. If your tower has a rectangular or circular base you probably just want to use Physics2D.OverlapBoxNonAlloc or Physics2D.OverlapCircleNonAlloc otherwise Physics2D.OverlapCollider inside Update should work. So you can iterate through all colliders that currently overlap with your tower and detemine if it overlaps with a certain collider.


The other solution is similar but you would use OnTriggerStay instead of OnTriggerEnter / Exit. At the end of each frame you would reset the placability flag to true, With OnTriggerStay you would simply set it to false. This should give you the right placability state before you reset it back to true.


Though if possible I would highly recomment the first solution since you have more control over when the checks are carried out.

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 Matthew_S · Aug 30, 2019 at 12:52 PM 0
Share

I had 3 different colliders for implacability under the same GameObject. Even when the tower had all parts inside of a single one of the colliders, and was nowhere near other colliders, it still returned inaccurate results. I think later I will move the colliders to different GameObjects so I can distinguish between water, road, and obstacles so I can have road/water specific towers or maybe pay to remove obstacles.

So in update on the phantomTower I can use Physics2D.OverlapCollider to find all overlapping colliers, then use CompareTag on all returned GameObjects to see the phantomTower collides with an unplaceable object?

I don't really understand Physics2D.OverlapBoxNonAlloc. From what I can tell, it is easier on the program by not storing the returned colliding GameObjects. Does it just return the number of colliding game objects? So then if the number>0 I should then use Physics2D.OverlapCollider to see what is colliding, and change the placability flag based on the result? Would it have a noticeable impact on performance if I just use Physics2D.OverlapCollider? There could only every be one phantomTower at a time, though Physics2D.OverlapCollider would be called every frame while placing a tower.

I think I tried resting the flag at the end of update. I think this made it so it never returns unplaceable unless I move the tower quickly to reduce frame rate. I read somewhere that having colliders move without a rigidbody can produces erroneous results and greatly reduce performance (but that's another problem). I have tried adding, removing, and changing the type of rigidbody on the phantomTower to no avail.

Using Physics2D.OverlapCollider seems like the simplest way for me to solve this problem.

OH. I also have no idea how to actually use any of these. I haven't been able to find what I actually need to put for any of these to work. Thank you very much!

avatar image
0

Answer by Matthew_S · Sep 14, 2019 at 06:56 PM

Turns out what I was doing actually worked just fine. The problem was that I had an if statement that overwrote it hidden away in a different part of the code for some reason.

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

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

Related Questions

How to set collision for an object with specific collider size? 3 Answers

Gameobject not detecting collison from other Box Collider 2D [SOLVED?] 2 Answers

My OnCollisionEnter says i have 128 Collisions every collision 1 Answer

How to make different actions using OnMouseDrag with two different colliders on one Game Object. 0 Answers

Problem with method Collider2D.isTouchingLayers() 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