- Home /
Game object z axis makes object not line up with parent
So Im having an issue, Im trying to place the cards into their individual cells, but, the cards z position makes it look as if its out of place for context the render of the canvas is screen space - camera and the field is tilted back a couple of degrees I dont want to hard code in values is there a better way?
is it the same issue as before or different?? recognize your background better approach on question this time, but still missing some key details. Soo... is that a prefab that is instantiating onto a parent or activating and deactivating a permanent in scene GO?
no its a different issue, before it wasnt following the muse, now whats happening is that it appears like its off center when in actuality its just not at a proper Z position when i move the card up a bit it looks right, i dont wana hard code any values, i know there has gotta be a better way. I know for fact it has everything to do with the fact that the board is leaned back. and once its on the scene its reactivated and that card variation type is a child of its detailed form, then it gets activated and set as a parent then kills the detail form.
ahh ok so is diff..yeah i was thinking the image on top didn't quite align with 0.x 0.y so i was assu$$anonymous$$g it was elevated myself... also not quite what you were looking for i assumed. you want it to be more or less 0.001f units away and above from the red Correct.. Centered and stretched to fit square? this shouldn't be toooo difficult.and may need a reference to each square to instantiate correctly. I do have to apologize though, i must crash, been up for about 30 hours now.. time to sleep.. hope someone can explain this to you better, Not sure they are on and willing but @bunny83 seems to have some pretty good insight on $$anonymous$$any different fields to do with c#. good luck and good night.
ps don't forget to upvote the best awnser(s) and close when complete :)
Bonne soirée.
Thamas out.
Answer by Bunny83 · Feb 21, 2019 at 12:56 PM
There are a lot things not clear, but i guess you use a perspective camera and your "field" gameobject has several children which represents a "cell". Note that the euler angles rotation values you see in the inspector are always local space rotation values. If you tilt your field object, the children will of course rotate with their parent. However their local space rotation does not change, only their absolute rotation.
If you parent the card to a slot there are two ways you can align the card:
set the cards localEulerAngle to (0,0,0) or set the localRotation to Quaternion.identity (which both do the same thing)
You can set the world space rotation of the card to match the worldspace rotation of the slot.
In the first case you would do
card.transform.parent = slot.transform;
card.transform.localPosition = Vector3.zero;
card.transform.localEulerAngle = Vector3.zero;
In the second case you would do
card.transform.position = slot.transform.position;
card.transform.rotation = slot.transform.rotation;
Note that the second case also works when the card is not parented to the slot.
If you want to offset the card slightly the first approach would be easier, since you can simply shift the card in localspace of the slot along the z axis. In worldspace this is more tricky (and slightly more unprecise). After parenting you can simply use this instead:
card.transform.localPosition = -Vector3.forward*0.01f;
So the card is moved to (0, 0, -0.01f) in local space. However it's generally better to just setup your slot gameobjects for the final position / rotation. So it's better to have the slot background as a seperate child and move those forward a bit instead of moving the card back. The background is just static environment. You have to work with the cards all the time, so it makes sense to setup the scene in a way it makes it easy to put the cards in.
Finally Unity has now the SetParent method it allows parenting and keeping the local orientation / position. When you assign the parent property Unity will actually recalculate the local position / rotation so the object stays at the same position / orientation as it was before. Using SetParent(slot.transform, false)
only makes sense if the card's local rotation and position is alreads the way you want it before you parent the object.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Dragging panel(prefab) from screen space canvas to world space canvas and scaling to fit 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer