Why does it matter if I use one GameObject's position or another's when drawing a mesh?
So I have the following code:
private GameObject wheel;
private GameObject barrel;
// Use this for initialization
void Start () {
barrel = new GameObject ("barrel");
barrel.AddComponent<MeshFilter>();
barrel.AddComponent <MeshRenderer>();
barrel.GetComponent<MeshRenderer> ().material = barrelMaterial;
wheel = new GameObject ("wheel");
wheel.AddComponent<MeshFilter>();
wheel.AddComponent <MeshRenderer>();
wheel.GetComponent<MeshRenderer> ().material = wheelMaterial;
DrawCannon ();
}
private void DrawCannon() {
// we need to draw both the cannon barrel and the wheel
// start with the wheel
// how detailed we want the wheel to be
List<Vector3> wheelVertices = new List<Vector3> ();
int wheelVerticesCount = 20;
float radius = 0.14f;
int multiple = isBallCannon ? -1 : 1;
// move the wheel to the position that we'd like it to be
barrel.transform.position = new Vector3 (multiple * 10f, -4f + radius,-0.2f);
wheel.transform.position = new Vector3 (multiple * 10f, -4f + radius,-0.2f);
float rads = Mathf.PI * 2 / (float)wheelVerticesCount;
// add the center vertex
wheelVertices.Add(transform.position);
for (int i = 0; i < wheelVerticesCount; i++) {
// add the next vertex along the circle
wheelVertices.Add(transform.position + radius *
new Vector3(Mathf.Cos(i*rads),Mathf.Sin(i*rads),0));
}
wheel.GetComponent<MeshFilter> ().mesh.vertices = wheelVertices.ToArray ();
// now that we have all the vertices of the circle object, we can draw them
int[] wheelTriangles = new int[wheelVerticesCount*3];
int j = 0;
for (int i = wheelVerticesCount; i > 0; i--) {
wheelTriangles [j] = i;
if (i > 1) {
wheelTriangles [j + 1] = i - 1;
} else {
wheelTriangles [j + 1] = wheelVerticesCount;
}
wheelTriangles [j + 2] = 0;
j += 3;
}
wheel.GetComponent<MeshFilter> ().mesh.triangles = wheelTriangles;
// now draw the barrel, it should just be a rectangle
List<Vector3> barrelVertices = new List<Vector3> ();
// we want this barrel to be just a bit taller than the wheel and about 3 time as long
barrelVertices.Add(new Vector3(barrel.transform.position.x-3*radius,barrel.transform.position.y + radius*1.2f,0f));
barrelVertices.Add(new Vector3(barrel.transform.position.x+3*radius,barrel.transform.position.y + radius*1.2f,0));
barrelVertices.Add(new Vector3(barrel.transform.position.x-3*radius,barrel.transform.position.y - radius*0.9f,0));
barrelVertices.Add(new Vector3(barrel.transform.position.x+3*radius,barrel.transform.position.y - radius*0.9f,0));
barrel.GetComponent<MeshFilter> ().mesh.vertices = barrelVertices.ToArray ();
// now that we have all the vertices of the circle object, we can draw them
int[] barrelTriangles = new int[6] {0,1,2,1,3,2};
barrel.GetComponent<MeshFilter> ().mesh.triangles = barrelTriangles;
}
This code is running in a script attached to an empty GameObject that is positioned at the origin. You'll note that to draw the wheel I work with the creating object's position and that seems to place the wheel where I want it to be. However, when I create the barrel and use what I actually think the position should be as a starting point for drawing the vertices, the barrel's actual position appears to be doubled in each of the axes, as can be seen in the image here:
When I print out the positions of both the wheel and the barrel GameObjects, their positions are shown to be exactly the same, which makes sense because that's the position I gave them in the code. So what's going on with the barrel? When I again use the creating GameObject's position to find the barrel's vertices, everything is fine. Neither is a child object of any other object, which is verified by looking at the heirarchy.
Answer by cancub · Feb 20, 2017 at 12:45 AM
I'm an idiot. When you move the position of the object, you no longer need to reference the origin when looking at the placement of vertices. Since the parent object was at the origin, I was adding (0,0,0) to the vertices, which is the correct thing to do after having moved the object (or just not add anything at all).