- Home /
Problems creating a grid in Runtime
Hey guys, I'm trying to create a grid using the mousedrag. My code is working more or less cause I've some strange behaviours. Can some one check my code and maybe give some ideas to make this better, and even make this works in a correct way ?
Thanks.
public class BuildingControl : MonoBehaviour {
public bool enableConstruction = true;
public Transform constructionBlock;
private Vector3 _startPoint;
private Vector3 _endPoint;
private Vector2 _squareSize;
private Vector2 _oldSquareSize;
private bool _constructionStarted;
private List<Transform> _cubes;
void Start ()
{
_cubes = new List<Transform>();
}
void CreateCubes ()
{
if (_oldSquareSize != _squareSize) {
foreach (Transform cube in _cubes) {
Destroy (cube.gameObject);
}
_oldSquareSize = _squareSize;
_cubes = new List<Transform> ();
}
for (int iCol = 0; iCol < _oldSquareSize.x; iCol++) {
for (int iRow = 0; iRow < _oldSquareSize.y; iRow++) {
Transform newCube = Instantiate (constructionBlock) as Transform;
Vector3 newPosition = Vector3.zero;
newPosition.x = (_startPoint.x >= _endPoint.x ? _startPoint.x - iCol : _endPoint.x + iCol);
newPosition.z = (_startPoint.z >= _endPoint.z ? _startPoint.z - iRow : _endPoint.z + iRow);
newPosition.y = 0.5f;
newCube.position = newPosition;
_cubes.Add (newCube);
}
}
}
void OnGUI ()
{
if (GUILayout.Button ("Enable Construction")) {
enableConstruction = !enableConstruction;
}
GUILayout.Label ("Start Point : " + _startPoint.ToString ());
GUILayout.Label ("End Point : " + _endPoint.ToString ());
GUILayout.Label ("SquareSize : " + _squareSize.ToString ());
}
void Update ()
{
RaycastHit hit;
if (Input.GetMouseButtonUp (0)) {
if (!_constructionStarted) {
if (Physics.Raycast (Camera.mainCamera.ScreenPointToRay (Input.mousePosition), out hit, 1000)) {
_startPoint = new Vector3 (Mathf.RoundToInt (hit.point.x), 0.5f, Mathf.RoundToInt (hit.point.z));
_constructionStarted = true;
_squareSize = new Vector2 (0, 0);
}
} else {
_startPoint = Vector3.zero;
_endPoint = Vector3.zero;
_squareSize = Vector2.zero;
_constructionStarted = false;
}
}
if (_constructionStarted) {
if (Physics.Raycast (Camera.mainCamera.ScreenPointToRay (Input.mousePosition), out hit, 1000)) {
_endPoint = new Vector3 (Mathf.RoundToInt (hit.point.x), 0.5f, Mathf.RoundToInt (hit.point.z));
_squareSize.x = Mathf.Abs (_endPoint.x - _startPoint.x);
_squareSize.y = Mathf.Abs (_endPoint.z - _startPoint.z);
}
}
if (_oldSquareSize != _squareSize) {
CreateCubes ();
}
}
}
Ok, you're right.
So, here are some shots.
When I first click, the script creates my first cube:
If I move down, I can have this doubled:
But when I move to the side, the script removes the first column, and starts to create from the second column:
If I go to the other side, I don't have this behaviour. Ok, not all behaviour, cause I lose the first column, but I think that it's the same problem. I'm trying to find out the real problem for myself, I believe that could be a stupid logic problem, but untill now, I'm still lost... :P
Can someone give me any ideas about this ?
Why not take the time to explain what "strange behaviours" means and describe in more detail, or use a screenshot or video, to make your problem easier to understand.
Answer by rkaetano · Jun 01, 2013 at 03:52 PM
Ok guys, I just found the solution for my problem. I just needed to change the cube creation For Loop This is the new loop:
for (int iCol = 0; iCol < _oldSquareSize.x; iCol++) {
for (int iRow = 0; iRow < _oldSquareSize.y; iRow++) {
Transform newCube = Instantiate (constructionBlock) as Transform;
Vector3 newPosition = _startPoint;
newPosition.x += (_startPoint.x >= _endPoint.x ? -iCol : +iCol);
newPosition.z += (_startPoint.z >= _endPoint.z ? -iRow : +iRow);
newPosition.y = 0.5f;
newCube.position = newPosition;
_cubes.Add (newCube);
}
}
I just don't know if this is the most performatic way to achieve the same mechanic. :P If someone can provide me any insights, i would be grateful.