I have a building placement project which works fine with Unity’s default 3D Objects, like 3D capsule, sphere, box objects however when I work on other 3D objects it doesn’t work. The objects all have rigidbody and box collider components while only the house prefab objects have child objects.
My scripts are like this:
public class BuildingManager : MonoBehaviour
{
private GameObject pendingObject;
[SerializeField] private Material[] materials;
public bool canPlace;
void Update()
{
if (pendingObject != null)
{
UpDateMaterials();
pendingObject.transform.position = pos;
if (Input.GetMouseButtonDown(0) && canPlace)
{
PlaceObject();
}
}
}
public void PlaceObject()
{
pendingObject.GetComponent<MeshRenderer>().material = materials[2];
pendingObject = null;
}
public void UpDateMaterials()
{
if (canPlace)
{
pendingObject.GetComponent<MeshRenderer>().material = materials[0];
}
if (!canPlace)
{
pendingObject.GetComponent<MeshRenderer>().material = materials[1];
}
}
}
public class CheckPlacement : MonoBehaviour
{
BuildingManager buildingManager;
void Start()
{
buildingManager = GameObject.Find("BuildingManager").GetComponent<BuildingManager>();
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Building"))
{
Debug.Log("Can't Place Building");
buildingManager.canPlace = false;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Building"))
{
Debug.Log("Can Place Building");
buildingManager.canPlace = true;
}
}
}
Any suggestions are welcome…