Problem with collision being detected too late in my building system

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Foundation_placement_checker : MonoBehaviour {

    public bool isFoundationPlacableX = true;
    public bool x;
    public bool colliding = false;

    private void Start()
    {
        x = isFoundationPlacableX;
    }

    private void OnTriggerStay(Collider other)
    {
        if(other.gameObject.tag == "Foundation_Prefab")
        {
            isFoundationPlacableX = false;

        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Foundation_Prefab")
        {
            isFoundationPlacableX = false;

        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Foundation_Prefab")
        {
            isFoundationPlacableX = true;

        }
    }
 
    public void PlaceFoundation(Vector3 pos, Quaternion rot, GameObject obj)
    {
        if(isFoundationPlacableX == true)
            Instantiate(obj, pos, rot);
    }
}

Foundation_Prefab is the center box, the other »invisible « objects with no mesh renderers are just for snapping.

For placing these Foundation_Prefabs I use another gameObject that is called Foundation_placeHolder

I move it around using raycasting (raycast from camera center point,10 units forward if it hits ground or one of the invisible gameObjects(»Snap points«) on Foundation_Prefab you can place it).

Foundation_placeHolder has Foundation_placement_checker script attached(Above script). (basicly what it does is check if it is colliding with Foundation_Prefab, if it is colliding with it, it turns variable isFoundationPlacableX to false, if its not colliding with it it turns isFoundationPlacableX to true)

Now I have this pice of code on another script called Building system.

if (Input.GetMouseButtonDown(0))
{
if (currentSelectedItem == 0 && isFoundationPlacable)
       {
           Vector3 pos = activeItemPlaceHolder.transform.position;
           Quaternion rot = activeItemPlaceHolder.transform.rotation;
                    activeItemPlaceHolder.GetComponent<Foundation_placement_checker>().PlaceFoundation(pos, rot, activeItem);

       }
}

So basicly what it does is: if User presses Mouse0 and some conditions are true it runs a method on Foundation_placement_checker script

activeItemPlaceHolder is basicly Foundation_placeHolder but under a different name.

pos and rot are the current position and rotation of activeItemPlaceHolder, activeItem is Foundation_Prefab.

public void PlaceFoundation(Vector3 pos, Quaternion rot, GameObject obj)
{
if(isFoundationPlacableX == true)
            Instantiate(obj, pos, rot);
}

BUG (Happens on the end of the gif):
https://gfycat.com/ApprehensiveNaughtyCoot
https://fat.gfycat.com/ApprehensiveNaughtyCoot.webm

So what iam trying to do? Have mouse right between snapping point and no snapping point (no snapping point – Foundation_placeHolder is inside Foundation_Prefab ). I click mouse when its Snapped(blue) and then quickly move it out of snapping point (red). If I time it just right i can place Foundation_Prefab inside another Foundation_Prefab.

So yea I’m not really sure how to fix it, Im not really sure whats causing it, but I think its because collision is being detected too late.

I’ve tried

setting fixed timestep to 0.001

setting Collision Detection to Continuous, Continuous Dynamic, interpolate, extrapolate

I do have another idea of doing this but dont wana rewrite whole building system just yet anyway lets see if anybody knows how to fix this.

One thing to try is this: at the same time you decide that it is placeable (in OnTriggerExit()), write down that position somewhere, then use it at the point of Instantiation. Then it doesn’t matter where things have gone afterwards because you should have a valid spawn point.