- Home /
The question is answered, right answer was accepted
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);
}
Ins$$anonymous$$d of moving individual bricks, can't you move the parent object left and right ins$$anonymous$$d?
I am moving the parent object only agter the foreach loop with
brick [0].transform.position = new Vector3 (brick [0].transform.position.x +xDirection, brick [0].transform.position.y);
In the foreach loop I check if the positions to the left or right of each part of the brick are free and in the playing field
Answer by izaazyunus · 9 $$anonymous$$utes ago @kire466 Got it. In that case, you can get the individual bricks coordinate in the parents coordinates by just adding the parent's position and the local position of the brick. For example if the parent is at (100,100) and a brick is at (10,-10) then the coordinates in the parent space is just (110,90)
I dont think we understood eachother. The problem i got is tat after the rotation, transform.position gives me a position that the brick does not occupy
Answer by kire466 · Oct 26, 2017 at 06:01 PM
I fixed it... the problem was in the conversion from float to int... putting
int newPosY = (int) Math.Round(bri.transform.position.y);
fixed my problem