So I am creating procedural meshes and trying to space them out by rotating them and then moving them. When I do it in the editor it works perfectly but when I implement this code it doesn’t rotate and then move, it moves and then rotates. Any one know why? and how to fix it.
block.transform.parent = transform;
block.transform.position = new Vector3(0, blockPos.y, 0);
float rotZ = blockPos.x / blockPos.y * 180;
float rotX = blockPos.z / blockPos.y * 180;
Quaternion rot = Quaternion.identity;
rot.eulerAngles = new Vector3(rotX, 0, rotZ);
block.transform.rotation = rot;
from the code you have listed mate you have the move frist, move the .position to after the .rotation.
Hope this helps.
yeah, it doesn’t mater which order you put it in, it does the same thing, i also tried using all forms of rotation,
sorry mate without seeing more code i cant help
well here is the whole function and the calling function, this piece of code gets called after the object is loaded.
void CreateLayer(int height)
{
tiles = new SuperBlock[height * 2 + 1][ ];
for(int z = -1;z <= 1;z++)
{
tiles[z + height] = new SuperBlock[height * 2 + 1];
for(int x = -1;x <= 1;x++)
{
tiles[z + height][x + height] = CreateBlock(new Vector3(x, height, z));
}
}
}
SuperBlock CreateBlock(Vector3 position)
{
Vector3 blockPos = position * 3;
GameObject block = new GameObject();
block.name = “block”;
block.transform.parent = transform;
block.transform.position = new Vector3(0, blockPos.y, 0);
float rotZ = blockPos.x / blockPos.y * 180;
float rotX = blockPos.z / blockPos.y * 180;
Quaternion rot = Quaternion.identity;
//rot.x = rotX;
//rot.z = rotZ;
//rot.y = 0;
//rot.w = 36;
rot.eulerAngles = new Vector3(rotX, 0, 0);
//block.transform.Rotate(new Vector3(rotX, 0, rotZ));
block.transform.rotation = rot; //Quaternion.Euler(new Vector3(rotX, 0, rotZ));
//block.transform.RotateAround(block.transform.forward, rotX);
SuperBlock sb = block.AddComponent();
sb.baseMaterial = baseMaterial;
sb.layerPos = position;
return sb;
}
If you want to move an object relative to its parent rotation you should use localPosition instead of position. If you want to move an object relative to its own rotation use block.transform.Translate(new Vector3(0, blockPos.y, 0), Space.Self) or block.transform.position = block.transform.parent.position + block.transform.rotation * new Vector3(0, blockPos.y, 0) or use Transform.TransformPoint. Of course you will have to rotate the object first…