Recently I started working on a puzzle game which main objective is to move laser beam source to designated location by using mirrors that will change the direction of laser. Very easy concept, however there will be more obstacles and more tools to use apart from mirrors.
So far I was able to create a custom grid that can be very easily modified. For instance if game requires 8x8 or 6x6 grid this can be changed by providing correct input for Building Grid script.
So far I was able to create a custom grid that can be very easily modified. For instance if game requires 8x8 or 6x6 grid this can be changed by providing correct input for Building Grid script.
// Constant offset value xOffSet = (float)10/gridColumns; yOffset = (float)10/gridRows; firstTile = CreateSingleGridTile(firstPoint, secondPoint, thirdPoint, fourthPoint, 0, 0); firstTile.renderer.enabled = false; GameObject grid = new GameObject(); // This method can be used from other Scripts to build extra grids, inventory systems etc BuildGrid (ref grid, gridRows, gridColumns, arrayOfTiles, "SingleTile", xOffSet, yOffset, zOffset, firstTile, firstPoint); // Set main beam position var positionOfBeamToAppear = arrayOfTiles[xMainBeam, yMainBeam]; mainLaserBeam.transform.position = new Vector3(positionOfBeamToAppear.transform.position.x -xOffSet/2, positionOfBeamToAppear.transform.position.y - yOffset/2, positionOfBeamToAppear.transform.position.z +zOffset); var positionOfLaserTargetToAppear = arrayOfTiles[xLaserTarget, yLaserTarget]; laserTarget.transform.position = new Vector3(positionOfLaserTargetToAppear.transform.position.x -xOffSet/2, positionOfLaserTargetToAppear.transform.position.y - yOffset/2, positionOfLaserTargetToAppear.transform.position.z +zOffset); } public void BuildGrid (ref GameObject grid, int gridRows, int gridColumns, GameObject[,] arrayOfTiles, string tagName, float xOffSet, float yOffset, float zOffset, GameObject firstTile, Vector3 initialBuildPosition ) { bool nextRow = false; for (int row = 0; row < gridRows; row++) { for (int column = 0; column < gridColumns; column++) { if (row == 0 && column == 0) { grid = Instantiate (firstTile, new Vector3 (initialBuildPosition.x, initialBuildPosition.y, initialBuildPosition.z - zOffset), Quaternion.identity) as GameObject; arrayOfTiles [row, column] = grid; grid.renderer.enabled = true; } else { if (!nextRow) { grid = Instantiate (arrayOfTiles [row, column - 1], new Vector3 (arrayOfTiles [row, column - 1].transform.position.x + xOffSet, initialBuildPosition.y, initialBuildPosition.z - zOffset), Quaternion.identity) as GameObject; } else { grid = Instantiate (arrayOfTiles [row - 1, column], new Vector3 (arrayOfTiles [row - 1, column].transform.position.x, arrayOfTiles [row - 1, column].transform.position.y - yOffset, arrayOfTiles [row - 1, column].transform.position.z), Quaternion.identity) as GameObject; } arrayOfTiles [row, column] = grid; grid.renderer.enabled = true; } // Based on name we can easily find the position of tile grid.name = string.Format ("{0}_{1}:{2}", tagName, row, column); grid.tag = tagName; grid.collider.enabled =true; } nextRow = true; } } // Update is called once per frame void Update () { } public GameObject CreateSingleGridTile(Vector3 firstPoint,Vector3 secondPoint,Vector3 thirdPoint,Vector3 fourthPoint, int xArrayIndex, int yArrayIndex ) { GameObject gridTile = new GameObject(string.Format("Reference_{0}:{1}", xArrayIndex,yArrayIndex) ); MeshFilter meshFilter = (MeshFilter)gridTile.AddComponent(typeof(MeshFilter)); meshFilter.mesh = CreateMesh(firstPoint,secondPoint, thirdPoint, fourthPoint); gridTile.AddComponent(); MeshRenderer renderer = gridTile.AddComponent(typeof(MeshRenderer)) as MeshRenderer; renderer.material.shader = Shader.Find ("Mobile/Particles/Additive"); renderer.material.mainTexture = tileTexture; float gridItemWidth = (float)1/gridColumns; float gridItemHeight = (float)1/gridRows; gridTile.transform.localScale = new Vector3(gridItemWidth,gridItemHeight,+zOffset); // Set grid Tile position // TODO add variable to calculate the grid difference gridTile.collider.enabled = false; return gridTile; } Mesh CreateMesh(Vector3 firstPoint, Vector3 secondPoint, Vector3 thirdPoint, Vector3 fourthPoint) { Mesh m = new Mesh(); m.name = "ScriptedMesh"; m.vertices = new Vector3[] { firstPoint, secondPoint, thirdPoint, fourthPoint }; m.uv = new Vector2[] { new Vector2 (0, 0), new Vector2 (1, 0), new Vector2(1, -1), new Vector2 (0, -1) }; m.triangles = new int[] { 0, 1, 2, 0,2,3}; //m.RecalculateNormals(); //m.Optimize(); return m; }
Comments
Post a Comment