- Home /
Changing build rotation
Hi guys, I am new to Unity and programmiing in general. My question is that i have written this script that builds a shape, in this case a pyramid, in a spiral motion. It all works well except, that i can't seem to turn the gameobject(brick) in the building direction the shape is going i've tried several things like changing it's lookrotation, fromtorotation, eulerangles.y but can't seem to get it to turn the right way. Any help would be appreciated. (If my code looks sloppy, i'm still learning) :)
public class PyramidBuilder : MonoBehaviour {
public int layer = 10;
public int cols = 10;
public int rows = 10;
float shiftBrickX = 0;
float shiftBrickY = 0;
float shiftBrickZ = 0;
//Quaternion brickRotation = Quaternion.identity;
// The gameobject of which the shape is made
public GameObject brick;
void BuildPyramid(){
// The Position of the gameobject relative to the shape that's created in the gameworld
shiftBrickX = brick.transform.position.x-5f;
shiftBrickY = brick.transform.position.y -1f;
shiftBrickZ = brick.transform.position.z-5f;
for(int i = 0;i < layer; i++){
// The amount the gameobject is moved on the next layer
shiftBrickX += .5f;
shiftBrickY += 1f;
shiftBrickZ += .5f;
// The Shape starts from left to right, than builds in a spiral motion.
for (float j = 0; j <= cols; j++) {
GameObject tempX = (GameObject)Instantiate (brick, new Vector3 (shiftBrickX + j , shiftBrickY, shiftBrickZ), Quaternion.identity);
tempX.transform.parent = transform;
if(j == cols){
for (float g = 1; g <= rows; g++) {
GameObject tempZ = (GameObject)Instantiate (brick, new Vector3 (shiftBrickX + j , shiftBrickY, (shiftBrickZ + g)), Quaternion.identity );
tempZ.transform.parent = transform;
if(g == rows){
for (float h = j-1; h > -1 ; h--) {
GameObject tempMinX = (GameObject)Instantiate (brick, new Vector3 (shiftBrickX + h, shiftBrickY, (shiftBrickZ + g)), Quaternion.identity);
tempMinX.transform.parent = transform;
if(h == 0){
for(float k = g-1; k > 0 ; k--){
GameObject tempMinZ = (GameObject)Instantiate (brick, new Vector3 (shiftBrickX + h, shiftBrickY, (shiftBrickZ + k)), Quaternion.identity);
tempMinZ.transform.parent = transform;
}
}
}
}
}
}
}
cols -= 1;
rows -= 1;
}
}
void Start () {
BuildPyramid();
}
Answer by anniyan137 · May 08, 2015 at 06:04 AM
Start() is called only once. It is like object initialization. You might want to put your code in Update() instead. This will let you rotate the object over a period of time.
Check this link for more info.
Answer by Irmo · May 08, 2015 at 01:24 PM
I found the answer. I overlooked the option of Quaternion.Euler, which lets you change the rotation on all 3 axes.
Your answer
Follow this Question
Related Questions
Instantiating prefab of object like it was in the scene 0 Answers
Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers
Objects Instantiating at wrong position 3 Answers
Instantiate question 1 Answer
Is there a way to set the current rotation of an object to 0,0,0? 2 Answers