- Home /
Clone positioning inside loop
Hello. I have an empty gameobject with a script attached. This script instantiates a set of boxes along X and Y with the use of a loop (looks like a solid brick wall). My problem is that I want to place many of these walls, and rotate them in the scene however I want, but no matter how I rotate the empty game object in order to rotate the WHOLE wall, the individual bricks just grab the local rotation and just use it as its own, so the wall is always oriented a certain way regardless of how i rotate the container. See screenshot
The problem is actually not the rotation, technically the bricks rotate the way i need them to. I think i need to MOVE them in relation to the last instantiated clone, but i dont know how to get a reference to that.
Clone script:
public Transform brick;
public GameObject spawn1;
private float x;
private float y;
private Vector3 pos2;
void Start()
{
for (y = 0; y < 10; y++)
{
for (x = 0 ; x < 10; x++)
{
Vector3 pos = transform.position;
// pos2.x = x + //some reference to last instantiated clone here?/;
// pos2.y = y + //some reference to last instantiated clone here?/;
// pos2.z = z + //some reference to last instantiated clone here?/;
Transform brickclone = Instantiate (brick, pos + new Vector3(x, y, 0), transform.rotation);
// brickclone.transform.position = ??
}
}
}
}
This instantiates a straight wall provided the container rotation is 0,0,0. Once i rotate the container, the individual bricks rotate but the wall is always oriented the same way. How do i get the whole wall to rotate based on the containers rotation and have the bricks line up straight?
Answer by FortisVenaliter · Feb 01, 2017 at 07:46 PM
You need to rotate the local positions when you are instantiating it. For example:
Transform brickclone = Instantiate (brick, pos + (transform.rotation * new Vector3(x, y, 0)), transform.rotation);