Detecion collision on specific object wall

I am new to Unity3d programming but I know a little bit C# programming. I have a little problem with my game. I want to detect collision between two objects only when they collision on specific wall. So I want to know is there any build in function or something like that in Unity to get that? I upload picture which contain what I want to obtain.

I want to detect collision only when walls in red circles will be close enough. Thank you for any help.

There’s no build in function that does that to my knowledge, but heres a work around:

  1. Select the object and create a new child object. (right click on it in the inspector → Create Empty).

  2. Select the new child object and add a Box Collider component. (Add Component → Physics → Box Collider).

  3. Click on Edit Collider on the Box Collider component. Here you can modify the size of the box and you can set it up to only cover the target wall. Play with the Center and Size options and the point handlers on the Scene View to get the right collider box. (The values on the picture might differ from yours)

  4. Them add a tag to the child.75625-5.png

  5. Finally put this function inside the object that is going to detect the collision.

    //Collision Detection
    void OnCollisionEnter(Collision col) {
    if (col.gameObject.CompareTag(“Wall”)) {
    //Your code here…

If you need more info about colliders go to unity documentation.

PS: I don’t know why the formating of the code doesn’t work, maybe a mod can help?


Thank you for your answer. I have one problem with this solution. In my “main” objects I have sphere collider (I need it to grab items using my mouse). When this new small collider (child) collide with another object it will push it back. Is there any way to prevent objects to do that?