Can place and Can't place issue

Hi,

I do want the program doesn’t let me place the building on top of other buildings. My project contains 3 types of 3D buildings with RigidBody and Box Colliders (Is Trigger are checked) that I select from the menu, and a Ground with Box Collider.

I tried to Debug.Log(“Can’t place”) within the if Statement on private void OnTriggerEnter(Collider other) but it get me nothing.

My scripts are like this:

public bool canPlace;

void Update()
{
        if(Input.GetMouseButtonDown(0) && canPlace)
        {
            PlaceObject();          
        }
}
public class CheckPlacement : MonoBehaviour
{
    BuildingManager buildingManager;
}

    void Start()
    {
        buildingManager = GameObject.Find("BuildingManager").GetComponent<BuildingManager>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Building"))
        {
            buildingManager.canPlace = false;
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Building"))
        {
            buildingManager.canPlace = true;
        }
    }
}

Thank you in advance…

theres not really enough info here, but you should be able to debug this… if canplace is not changing like you expect then well, whatever you are using to cause the trigger, isnt meeting a building…

1 Like

What is the close brace in line 4 on your second block?

Does any of this even compile? It sure doesn’t look like it to me.

Sorry, I must have made a mistake when I pasted the script. The CheckPlacement script is like:

public class CheckPlacement : MonoBehaviour
{
    BuildingManager buildingManager;

    private void Start()
    {
        buildingManager = GameObject.Find("BuildingManager").GetComponent<BuildingManager>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Building"))
        {
            Debug.Log("X");
            buildingManager.canPlace = false;
        }
    }
   private void OnTriggerExit(Collider other)
   {
        if (other.gameObject.CompareTag("Building"))
        {
            Debug.Log("V");
            buildingManager.canPlace = true;
        }
   }
}

While the whole BuildingManager script is like:

public class BuildingManager : MonoBehaviour
{
    public GameObject[] buildings;
    private GameObject pendingObject;

    private Vector3 pos;
    private RaycastHit hit;
    [SerializeField] private LayerMask layerMask;

    private float mouseWheelRotation;

    public bool canPlace;
   
    private void Update()
    {
        if (pendingObject != null)
        {
            MoveCurrentObjectToMouse();
            RotateFromMouseWheel();

            pendingObject.transform.position = pos;

            if (Input.GetMouseButtonDown(0) && canPlace)
            {
                PlaceObject();
            }
        }
    }

    private void MoveCurrentObjectToMouse()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 1000, layerMask))
        {
            pos = hit.point;
            pendingObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
        }
    }
   
    private void RotateFromMouseWheel()
    {
        mouseWheelRotation += Input.mouseScrollDelta.y;
        pendingObject.transform.Rotate(Vector3.up, mouseWheelRotation * 10f);
    }
    public void PlaceObject()
    {   
        pendingObject = null;
    }

    public void SelectObject(int index)
    {
        pendingObject = Instantiate(buildings[index], pos, transform.rotation);
    }
}

Keep going! Start printing more stuff… print the tag before you check it… print the name of the thing coming in… print the name of other… it’s up to YOU to figure out what is running and what isn’t running. Find out if the collision event is even being called at all, not just if the tag matches. This is just basic debugging, which I know I have suggested to you before.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like

I need to add the CheckPlacement script into the prefabs and figured it out how to adjust the bounding volume of Box Collider component of the prefabs. Editing the bouncing volume is really a challenging one:)

Thank you so much…

Anyway, sometimes the buildings still overlap.