Search Unity

Prefab Placment Probelm

Discussion in 'Scripting' started by Bluefire1234, Jun 27, 2021.

  1. Bluefire1234

    Bluefire1234

    Joined:
    Feb 26, 2021
    Posts:
    11
    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):
    1.  
    2.  
    3. using System.Collections;
    4.  
    5. using System.Collections.Generic;
    6.  
    7. using UnityEngine;
    8.  
    9.  
    10. public class BuildingBlocks : MonoBehaviour
    11.  
    12. {
    13.  
    14.  
    15.  
    16.     public Material templateMaterial;
    17.  
    18.     public Material templateYellow;
    19.  
    20.     public Material templateBlue;
    21.  
    22.     public bool buildModeOn = false;
    23.  
    24.     public bool building = false;
    25.  
    26.     private Vector3 buildPosition;
    27.  
    28.     private Vector3 point;
    29.  
    30.     public float distance = 0;
    31.  
    32.     public float allowedDistance;
    33.  
    34.  
    35.     private GameObject currentTemplateBlock;
    36.  
    37.     private GameObject newBlock;
    38.  
    39.  
    40.     [SerializeField]
    41.  
    42.     private GameObject[] blockTemplatePrefab;
    43.  
    44.     [SerializeField]
    45.  
    46.     private GameObject[] blockPrefab;
    47.  
    48.     [SerializeField]
    49.  
    50.     private LayerMask buildableSurfacesLayer;
    51.  
    52.  
    53.     public float curDist = 1000f;
    54.  
    55.     GameObject ItemToDestroy;
    56.  
    57.  
    58.     public int BlockNumber;
    59.  
    60.  
    61.     // Use this for initialization
    62.  
    63.     void Start()
    64.  
    65.     {
    66.  
    67.         buildModeOn = true;
    68.  
    69.     }
    70.  
    71.  
    72.     // Update is called once per frame
    73.  
    74.     void Update()
    75.  
    76.     {
    77.  
    78.  
    79.        
    80.  
    81.  
    82.  
    83.         GameObject[] allBlocks;
    84.  
    85.         allBlocks = GameObject.FindGameObjectsWithTag("PlacedBlock");
    86.  
    87.         RaycastHit hitInfo = new RaycastHit();
    88.  
    89.         bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 20, buildableSurfacesLayer);
    90.  
    91.        
    92.  
    93.         if (Input.GetKeyDown("b"))
    94.  
    95.         {
    96.  
    97.             buildModeOn = !buildModeOn;
    98.  
    99.         }
    100.  
    101.  
    102.         if (buildModeOn)
    103.  
    104.         {
    105.  
    106.             if (hit)
    107.  
    108.             {
    109.  
    110.                 point = hitInfo.point;
    111.  
    112.                 buildPosition = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y) + (0.5f), Mathf.Round(point.z));
    113.  
    114.                 building = true;
    115.  
    116.  
    117.             }
    118.  
    119.             else
    120.  
    121.             {
    122.  
    123.                 if(currentTemplateBlock != null)
    124.  
    125.                 Destroy(currentTemplateBlock.gameObject);
    126.  
    127.                 building = false;
    128.  
    129.             }
    130.  
    131.  
    132.         }
    133.  
    134.  
    135.         if (!buildModeOn && currentTemplateBlock != null)
    136.  
    137.         {
    138.  
    139.             Destroy(currentTemplateBlock.gameObject);
    140.  
    141.             building = false;
    142.  
    143.         }
    144.  
    145.  
    146.         if (building && currentTemplateBlock == null)
    147.  
    148.         {          
    149.  
    150.            
    151.  
    152.             currentTemplateBlock = Instantiate(blockTemplatePrefab[BlockNumber], buildPosition, Quaternion.identity);
    153.  
    154.             currentTemplateBlock.GetComponent<MeshRenderer>().material = templateMaterial;
    155.  
    156.            
    157.  
    158.              
    159.  
    160.         }
    161.  
    162.         if (building && currentTemplateBlock != null)
    163.  
    164.         {
    165.  
    166.             currentTemplateBlock.transform.position = buildPosition;
    167.  
    168.  
    169.             if (Input.GetMouseButtonDown(0))
    170.  
    171.             {
    172.  
    173.                 PlaceBlock();
    174.  
    175.             }
    176.  
    177.             for (int i = 0; i < allBlocks.Length; i++)
    178.  
    179.             {
    180.  
    181.                 //for larger blocks in this case its only block 1
    182.  
    183.                
    184.  
    185.                 if (BlockNumber == 1)
    186.  
    187.                 {
    188.  
    189.                     allowedDistance = 4.9f;
    190.  
    191.                    
    192.  
    193.                 }
    194.  
    195.                 else allowedDistance = 1.414214f;
    196.  
    197.  
    198.  
    199.                 distance = Vector3.Distance(allBlocks[i].gameObject.transform.position, currentTemplateBlock.transform.position);
    200.  
    201.                 if (distance == 1 || distance - allowedDistance < 0.03)
    202.  
    203.                 {
    204.  
    205.                     Debug.Log("Block and temp block next to each other");
    206.  
    207.                     currentTemplateBlock.GetComponent<MeshRenderer>().material = templateBlue;
    208.  
    209.                     break;
    210.  
    211.                 }
    212.  
    213.                 else currentTemplateBlock.GetComponent<MeshRenderer>().material = templateYellow;
    214.  
    215.             }
    216.  
    217.         }
    218.  
    219.         if(Input.GetKeyDown(KeyCode.Z))
    220.  
    221.         {
    222.  
    223.             if(allBlocks.Length != 0)
    224.  
    225.                 Destroy(allBlocks[allBlocks.Length - 1].gameObject);
    226.  
    227.         }
    228.  
    229.  
    230.         if (Input.GetKeyDown(KeyCode.Delete) || Input.GetMouseButtonDown(1))
    231.  
    232.         {
    233.  
    234.             foreach(GameObject Block in allBlocks)
    235.  
    236.             {  
    237.  
    238.                
    239.  
    240.                 float dist = Vector3.Distance(Block.transform.position, currentTemplateBlock.transform.position);
    241.  
    242.            
    243.  
    244.                 if(dist <= curDist)
    245.  
    246.                 {
    247.  
    248.                     curDist = dist;
    249.  
    250.                     ItemToDestroy = Block;
    251.  
    252.                 }
    253.  
    254.             }
    255.  
    256.         }
    257.  
    258.  
    259.         if (ItemToDestroy != null && distance <= allowedDistance)
    260.  
    261.         {
    262.  
    263.             Destroy(ItemToDestroy);
    264.  
    265.             curDist = 100000;
    266.  
    267.         }
    268.  
    269.        
    270.  
    271.         if (Input.GetKey("escape"))
    272.  
    273.         {
    274.  
    275.             Application.Quit();
    276.  
    277.         }
    278.  
    279.  
    280.  
    281.  
    282.     }
    283.  
    284.     private void PlaceBlock()
    285.  
    286.     {
    287.  
    288.         Debug.Log("Placing block");
    289.  
    290.         newBlock = Instantiate(blockPrefab[BlockNumber], buildPosition, Quaternion.identity);      
    291.  
    292.     }
    293.  
    294.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
    Bluefire1234 likes this.
  3. Bluefire1234

    Bluefire1234

    Joined:
    Feb 26, 2021
    Posts:
    11
    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
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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."