- Home /
Having trouble adding offsets to my grid
I would like to add some offsets to my points in my grid but I don't know how. I made it where it had to generate a grid full of cubes from the center but ever since adding that I am unable to figure out how to implement the offset part, I'm trying to do this in the pic.
Example Pic:
Code:
public class Grid : MonoBehaviour {
public Vector3[] points;
public int xSize = 5;
public int zSize = 5;
public float offset = 1;
// Use this for initialization
void Start () {
points = new Vector3[zSize * xSize];
for(int i = 0, z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++, i++)
{
points[i] = new Vector3(x - (xSize / 2f), z - (zSize / 2));
print("There are " + points[i]);
}
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.black;
for (int i = 0; i < points.Length; i++)
{
Gizmos.DrawSphere(points[i], 0.1f);
}
}
// Update is called once per frame
void Update () {
}
}
for(int i = 0, z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++, i++)
{
points[i] = new Vector3(x - (xSize / 2f), z - (zSize / 2));
print("There are " + points[i]);
}
}
This will give you out of range index. Should be:
for(int i = 0, z = 0; z < zSize - 1; z++)
for (int x = 0; x < xSize - 1; x++, i++)
And you should make the code increment value of original position + offset inside loop. Ins$$anonymous$$d of making a new Vector3 inside loop
I managed to do what I wanted to do by using this:
points[i] = new Vector3(x offset - (xSize (offset 0.5f) - (offset / 2)), z offset - (zSize (offset 0.5f) - (offset / 2)));
Answer by KalleRosendahl · Jul 03, 2018 at 09:27 PM
isnt it just to multiply by 2?
points[i] = new Vector3((x - (xSize / 2f))*2, (z - (zSize / 2))*2);
that should make the offsets twice as large ie 2 units instead of 1
Your answer
Follow this Question
Related Questions
Creating an Object with a string C#! How? 1 Answer
Grid Placement In Game 2 Answers
Distribute terrain in zones 3 Answers
Improving Grid Algorithm 1 Answer
Multiple Cars not working 1 Answer