Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Grid problem with different size of buildings

Discussion in 'Scripting' started by Dino1997, May 4, 2014.

  1. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    Hello everyone,I'm making a city building game and I'm stuck on with a grid problem.

    I have basic grid working but my problem is that it doesn't work good with different sizes.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Builder : MonoBehaviour {
    6.  
    7.     public GameObject buildObject;
    8.  
    9.     public int gridSize;
    10.  
    11.     public Vector3 position;
    12.  
    13.     private GameObject ghostBuilding;
    14.  
    15.     // Use this for initialization
    16.     void OnEnable() {
    17.         ghostBuilding = Instantiate(buildObject, position, Quaternion.identity) as GameObject;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.         RaycastHit hit;
    25.  
    26.         if(Physics.Raycast(ray, out hit)) {
    27.             Debug.DrawLine(transform.position, hit.point);
    28.  
    29.             float posX = Mathf.Round(hit.point.x / gridSize) * gridSize;
    30.             float posZ = Mathf.Round(hit.point.z / gridSize) * gridSize;
    31.  
    32.             position = new Vector3(posX, 0, posZ);
    33.  
    34.             Debug.DrawLine(transform.position, new Vector3(posX, 0, posZ), Color.red);
    35.  
    36.             ghostBuilding.transform.position = position;
    37.  
    38.             if(Input.GetMouseButtonUp(0)) {
    39.                 Instantiate(buildObject, position, Quaternion.identity);
    40.                 this.enabled = false;
    41.                 Destroy(ghostBuilding);
    42.             }
    43.         }
    44.     }
    45. }
    46.  
    47.  

    Grid works great when building is same size as grid size(1x1),but when I put 2x2 building it doesn't follow the grid(It works on 3x3 so I guess it only doesn't work on even numbers).


    $Problem.jpg



    This problem is bugging me for a few days ,I have tried everything but I cannot fix this.

    Any help is appreciated.
     
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Put your origin at a corner, not the middle of the building. Or change the points depending on building width ;)
     
  3. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    Now I feel stupid,I tried moving everything,but it was that simple :D Thanks for your help :)