Problem retrieving transform.position after parent rotation
Hello, I am trying to make a Tetris clone to practice a bit but I stumbled upon a problem I can't resolve. I spawn a brick, for example the L shaped one,made from gameobjects representing each one piece of the brick. I make a piece the parent of all the other pieces in order to rotate the brick around this piece with a simple transform.roation. When I rotate it everything looks fine, but when I try to move it, so I go and check if the brick can move, transform.position for one of the child gets me the wrong value, giving me a free position as a position occupied by that piece. This only happens when trying to move on the field with the x value equal to 0. My tetris field where i can move the bricks is 10 positions in width and 20 positions in height. Its seems to me as if transform.position is returning me the local value insted of the gobal when i check for positions with the x = 0.
The code that follows is how i check and move the brick, with brick[0] the parent of the other pieces. I rotate the brick with a simple
brick [0].transform.Rotate (0f, 0f, 90f); ...
void MoveBrick(int xDirection){
foreach (GameObject bri in brick) {
int newPosX =(int) bri.transform.position.x + xDirection;
int newPosY = (int)bri.transform.position.y;
if (newPosX < 0 || newPosX >= columns)
return;
if (newPosY<rows&&placeTaken [newPosX-originX, newPosY-originY])
return;
}
brick [0].transform.position = new Vector3 (brick [0].transform.position.x +xDirection, brick [0].transform.position.y);
}
Your answer
Follow this Question
Related Questions
I want to make a Cube surface 0 Answers
Canon Ball shooting with Instiate 1 Answer
transform.lookat not working? 0 Answers
Auto level an object 0 Answers
GameObject not looking at me.. 1 Answer