- Home /
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?
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.
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!
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.
Your answer
Follow this Question
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