Hey making a grid based placement system which works fine but when I use a bigger prefab they glitch into each other pls help https://paste.myst.rs/xg3tg1lj Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class BuildingBlocks : MonoBehaviour { public Material templateMaterial; public Material templateYellow; public Material templateBlue; public bool buildModeOn = false; public bool building = false; private Vector3 buildPosition; private Vector3 point; public float distance = 0; public float allowedDistance; private GameObject currentTemplateBlock; private GameObject newBlock; [SerializeField] private GameObject[] blockTemplatePrefab; [SerializeField] private GameObject[] blockPrefab; [SerializeField] private LayerMask buildableSurfacesLayer; public float curDist = 1000f; GameObject ItemToDestroy; public int BlockNumber; // Use this for initialization void Start() { buildModeOn = true; } // Update is called once per frame void Update() { GameObject[] allBlocks; allBlocks = GameObject.FindGameObjectsWithTag("PlacedBlock"); RaycastHit hitInfo = new RaycastHit(); bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 20, buildableSurfacesLayer); if (Input.GetKeyDown("b")) { buildModeOn = !buildModeOn; } if (buildModeOn) { if (hit) { point = hitInfo.point; buildPosition = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y) + (0.5f), Mathf.Round(point.z)); building = true; } else { if(currentTemplateBlock != null) Destroy(currentTemplateBlock.gameObject); building = false; } } if (!buildModeOn && currentTemplateBlock != null) { Destroy(currentTemplateBlock.gameObject); building = false; } if (building && currentTemplateBlock == null) { currentTemplateBlock = Instantiate(blockTemplatePrefab[BlockNumber], buildPosition, Quaternion.identity); currentTemplateBlock.GetComponent<MeshRenderer>().material = templateMaterial; } if (building && currentTemplateBlock != null) { currentTemplateBlock.transform.position = buildPosition; if (Input.GetMouseButtonDown(0)) { PlaceBlock(); } for (int i = 0; i < allBlocks.Length; i++) { //for larger blocks in this case its only block 1 if (BlockNumber == 1) { allowedDistance = 4.9f; } else allowedDistance = 1.414214f; distance = Vector3.Distance(allBlocks[i].gameObject.transform.position, currentTemplateBlock.transform.position); if (distance == 1 || distance - allowedDistance < 0.03) { Debug.Log("Block and temp block next to each other"); currentTemplateBlock.GetComponent<MeshRenderer>().material = templateBlue; break; } else currentTemplateBlock.GetComponent<MeshRenderer>().material = templateYellow; } } if(Input.GetKeyDown(KeyCode.Z)) { if(allBlocks.Length != 0) Destroy(allBlocks[allBlocks.Length - 1].gameObject); } if (Input.GetKeyDown(KeyCode.Delete) || Input.GetMouseButtonDown(1)) { foreach(GameObject Block in allBlocks) { float dist = Vector3.Distance(Block.transform.position, currentTemplateBlock.transform.position); if(dist <= curDist) { curDist = dist; ItemToDestroy = Block; } } } if (ItemToDestroy != null && distance <= allowedDistance) { Destroy(ItemToDestroy); curDist = 100000; } if (Input.GetKey("escape")) { Application.Quit(); } } private void PlaceBlock() { Debug.Log("Placing block"); newBlock = Instantiate(blockPrefab[BlockNumber], buildPosition, Quaternion.identity); }
Not sure how you are deconflicting the objects, but you need to consider their shape to deconflict them properly. Take a look at tutorials for doing things like Tetris because obviously they have irregularly-shaped objects.
The objects are all just cubes just blockprefab 1 is bigger as you can see in line at line 185. I am checking if it is this Cube
Then make a tiny board, tiniest that shows the problem, and start outputting the values with Debug.Log() until you figure out why the decision is "yes you can place here" when it should be "no you cannot."