Hello!
I want to register collision when it happens inside BoxCollider2D and instantiate hit effect at that point, but when I try to determine the point using collision.GetContact(0).point I receive the point which is actually outside of the box collider.
I don’t think it’s possible to register a collision when it happens inside a BoxCollider2D, or any collider. You may need to add an offset to the collision point to get the effect to play inside the collider.
If people know better, I hope they’ll come here and help.
When a collider is inside another, the whole area of the of the one inside it is a contact so what is it specifically you want? For instance, the point at which you instantiate the “thing” inside the box is also a contact point so it’s not clear what you’re trying to get from the physics system.
To note, contact points are points where forces are applied to separate bodies/colliders and not simply points of intersection. With that known, you can use Physics2D.Distance which gives you points of intersection and normal and a property indicating if the distance forms an overlap which is also indicated by a negative distance.
Solved this by adding kinda duct tape code
I added a trigger collider to my FirePoint object and if it’s inside of any collider I set isInsideCollider variable to true.
And then, in the bullet script I added a condition:
Not sure if it’ll be useful but know you can easily do this kind of stuff using queries too. The advantage of queries is that you don’t have to write scripts for the specific callbacks and can perform the action whenever you like.
I think the problem was in the way I instantiate hit effect. At first bullet appears inside the collider and only then OnCollision method works shifting bullet position to the edge of BoxCollider2D (which is logical since bullet has Rigidbody2D on board). That’s why hit effect instantiates at the edge, but not inside. All I had to do is to add a condition inside the bullet script and destroy the bullet with hit effect right before it starts moving.