I am trying to see if two GameObjects with a Box Collider 2D is touching/overlapping. Since I only need to worry about this when the user click, I decided that using OnCollisionEnters2D or other function based checking is not necessary, Hence I am trying to use IsTouching or IsTouchingLayers.
I version of the code I tried with IsTouching is:
var towers = GameObject.FindGameObjectsWithTag("Tower");
for (var tower : GameObject in towers) {
if (GetComponent(Collider2D).IsTouching(tower.GetComponent(Collider2D))) {
Debug.Log("Touch");
}
}
And I tried with IsTouchingLayers:
var layer : LayerMask = LayerMask.GetMask("Towers");
if (GetComponent(Collider2D).IsTouchingLayers(layer)) {
Debug.Log("Touch");
}
The objects I am trying to see if touching is tagged with “Tower” and in the layer “Towers”. However, both code never returned true even though they are clearly overlapping. I also tried switching “Is Trigger” on and off both the objects with no luck…
there are caveats on the API pages for those about how/when the value is updated being based off the “last physics step”… so I’m guessing timing might be involved or is there time between the last position update and the click?
I am not sure about the timing of the physics step, but none of the objects were moving. So unless physics isn’t running at all… or take over like 10 seconds to update? I don’t really think timing could be an issue.
Tower objects never moves, cause you know, they are towers, and the object this script is running on is linked to the mouse position.
the physics engine deals with interaction within rigidbodies and between rigidbodies and colliders; no rigidbody involved, the physic engine doesn’t know anything happened.