Hey! Ive made one script class called “Square” in C#, which got following constructor:
public Square(float x, float y, float width,float length, UnityEngine.GameObject go)
And in the grid class, i am dividing the terrain into square objects and taking them in a Square[ , ] array.
Everything works well, when i test in debug.log the array contains all the squares of the terrain. The problem is when i want it drawn on the terrain as a grid.
I was planning to use lineRenderer to draw the grid so in the Start() method i got the following:
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.SetVertexCount(5);
if(lineRenderer == null)
{
Debug.Log ("null lineRenderer");
}else {Debug.Log("lineRenderer");}
Ive tried the following in the Update() method, but it doesnt work:
void Update()
{
for(int i=0; i<mXCells; i++)
{
for(int j=0; j<mYCells; j++)
{
Vector3 pos0 = new Vector3(mSquare[i,j].getX(), zOffset, mSquare[i,j].getY());
Vector3 pos1 = new Vector3(mSquare[i,j].getXPlus(), zOffset, mSquare[i,j].getY() );
Vector3 pos2 = new Vector3(mSquare[i,j].getXPlus(), zOffset, mSquare[i,j].getYPlus() );
Vector3 pos3 = new Vector3(mSquare[i,j].getX(), zOffset, mSquare[i,j].getYPlus() );
lineRenderer.SetWidth(10,10);
lineRenderer.SetColors(Color.red, Color.red);
lineRenderer.SetPosition(0, pos0);
lineRenderer.SetPosition(1, pos1);
lineRenderer.SetPosition(2, pos2);
lineRenderer.SetPosition(3, pos3);
lineRenderer.SetPosition(4, pos0);
}
}
Debug.Log ("Rendered Line");
}
I cant work out whats wrong, why the lines of all the squares arent drawn on the terrain so it becomes a grid. So it would be greate if someone got an idea of whats wrong.
Because it works if i just want the square/tile that the mouse is above to drawn by having this in the Update() method:
function Update ()
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//INEFFICIENT, CONSTANT POLLING, BUT WORKS
///////////////////////////////////////////////////////////////////////////////////////////////////////
//Debug.Log("Mouse is over the terrain");
//GET THE MOUSE POSITION
var mousePos : Vector3 = Input.mousePosition;
//CREATE A RAY AT THE MOUSE POSITION
var ray : Ray = mCamera.ScreenPointToRay (mousePos);
var hit : RaycastHit;
//WHEN THE RAY IS CAST
if (Physics.Raycast (ray, hit))
{
//STORE THE POINT WHERE IT HITS
var hitPoint : Vector3 = hit.point;
//SEARCHES THROUGH THE CELLS ON THE X AXIS
for (var i = 0; i < mXCells; i++)
{
//SEARCHES THROUGH THE CELLS ON THE Y AXIS
for (var j = 0; j < mYCells; j++)
{
//IF THE HIT POINT OF THE RAY IS BETWEEN THE BOUNDS OF A TILE
(
( hitPoint.x > mTiles[i,j].GetX() )
( hitPoint.z > mTiles[i,j].GetY() )
( hitPoint.x < ( mTiles[i,j].GetX() + mTiles[i,j].GetWidth() ) )
( hitPoint.z < ( mTiles[i,j].GetY() + mTiles[i,j].GetLength() ) ) )
{///checks through all the tiles to see if the ray casted is between the four sides of the tile
var pos0 : Vector3 = Vector3( mTiles[i,j].GetX(), zOffset, mTiles[i,j].GetY() );
var pos1 : Vector3 = Vector3( mTiles[i,j].GetXPlus(), zOffset, mTiles[i,j].GetY() );
var pos2 : Vector3 = Vector3( mTiles[i,j].GetXPlus(), zOffset, mTiles[i,j].GetYPlus() );
var pos3 : Vector3 = Vector3( mTiles[i,j].GetX(), zOffset, mTiles[i,j].GetYPlus() );
lineRenderer.SetWidth(10,10);
lineRenderer.SetColors(Color.blue, Color.blue);
//set the lines being drawn
lineRenderer.SetPosition(0, pos0);
lineRenderer.SetPosition(1, pos1);
lineRenderer.SetPosition(2, pos2);
lineRenderer.SetPosition(3, pos3);
lineRenderer.SetPosition(4, pos0);
}
}
}
}
}
Then it all works, then the tile where the mouse points is drawn, but as fast as i remove the ray if tests because i want all the tiles being drawn all the time, then it wont work:S
-HeadBlast, trying making a ladder game:)