- Home /
Instantiate a GameObject at the position of one of its child objects
I'm working on a script to randomly generate a dungeon by connecting random rooms with each other based on their door positions. See picture:
To the left is an example of a room GameObject - containing a lot of child GameObjects being all the contents of the room. This includes its doors, depicted above. A room can have 1-4 doors, and I wish to instantiate new rooms at the positions of these doors (see picture in the middle).
My issue is that when I instantiate a new room GameObject, its pivot point is located in the center of the room - and thus, I don't know how to align it with the doors. I can set its height correctly by instantiating it at the location of the original room, then moving it up as much as the room is long. However, I cannot get the horizontal position to work since I don't know how to match up the door position (see picture to the right).
If I could tell it to be instantiated using the door location as its pivot, everything would work fine, but I don't know if this is possible or if it's the best solution. Any suggestions?
Answer by Fattie · Dec 26, 2012 at 05:15 PM
Sure, just use four markers
Funnily enough someone just asked about this:
http://answers.unity3d.com/questions/369013/is-there-any-way-to-track-the-position-of-a-certai.html
So your Room thing will have four markers, that is, empty game objects, and we'll call them doorMarkerA, doorMarkerB, doorMarkerC, doorMarkerD.
So you're about to position a new room. we'll call it NEWROOM.
Let's say you want the CENTER of NEWROOM to be at point PLACEITHERE
of course, you just do this
NEWROOM .. get it from your pool
NEWROOM.transform.position = PLACEITHERE
So that's obvious. Now, if you want DOOR "A" of the NEWROOM to be at the point PLACEITHERE,
Here is what you do:
NEWROOM .. get it from your pool
NEWROOM.transform.position = PLACEITHERE
NEWROOM.transform.position -= doorMarkerA.localPosition
So that's exactly how you do it. You SUBTRACT the LOCAL POSITION of the relevant marker.
Hope it helps!
I'm afraid I cannot get it to work. I get the target door position:
GameObject targetDoor = newRoom.GetComponent().northDoor;
Then its local X position (the Y position is always the same for all rooms, and the Z position is easy to deter$$anonymous$$e otherwise):
float targetDoorX = targetDoor.transform.localPosition.x;
Then the final X position of the new room:
float targetX = newRoom.transform.position.x-targetDoorX;
And finally move the room (the Y and Z positions are deter$$anonymous$$ed elsewere and are correct):
newRoom.transform.position = new Vector3(targetX, transform.position.y, targetZ);
But the new room's X position is still wrong:
https://dl.dropbox.com/u/7821237/roomposition.jpg
Any idea what I am doing wrong?
(1) Did you get a chance to do exactly what I said here...
"So your Room thing will have four markers, that is, empty game objects, and we'll call them door$$anonymous$$arkerA, door$$anonymous$$arkerB, door$$anonymous$$arkerC, door$$anonymous$$arkerD."
So, you must ADD four new E$$anonymous$$PTY GA$$anonymous$$E OBJECTS to your prefab or model.
Name them door$$anonymous$$arkerA, door$$anonymous$$arkerB, door$$anonymous$$arkerC, door$$anonymous$$arkerD.
They should simply be empty game objects.
in the editor, move them around so they are perfectly on top of the middle of the four doors.
Have you had a chance to do this?
I did, and it still seemed to have issues. However, after doing some further testing, subtracting the local position of the new door and also adding the position of the first, connecting door seems to result in the correct position. Thanks for your help :)