I have a number of objects, using playing cards for test, on top of each other like this:
The objects are perfectly lined out and then i click on Heart Queen (HQ) and get the following:
When i click on HQ i do make the HQ parent and the other yellow is child’s as well as increase their sort order to get them on top. Here is the code i use for this:
print ("Here we go - " + MainScript.selectedGameObjectList.Count);
firstRound = true;
// Allocate all selected objects to the container
foreach(GameObject aGameObject in MainScript.selectedGameObjectList) {
if(firstRound) {
// At the first round do not add child as the first object is parent
theFirstGO = aGameObject;
// theFirstGO.transform.renderer.sortingOrder = 100;
firstRound = false;
}
else {
// Add to parent
print (aGameObject.name + " / " + aGameObject.transform.position);
aGameObject.transform.parent = theFirstGO.transform;
aGameObject.collider2D.enabled = false;
}
}
// Make sure the selected container, with all it's objects, get's on top
theMainScript = new MainScript(); // Set up access to MainScript
GameObject dummyGO;
int lengthOfList = MainScript.selectedGameObjectList.Count;
for(int zz = 0; zz < MainScript.selectedGameObjectList.Count; zz++) {
MainScript.selectedGameObjectList[zz].transform.renderer.sortingOrder = 100; //transform.renderer.sortingOrder;
}
However, my challenge is how to, except for the selected object (HQ) which get’s on top, keep the position for the child’s the parent after selection? I know there is some functions for some type of conversion like world to local etc. but have not been able to figure out how to do this?
Worst case i guess this needs to be done manually for each selected object, which i tried but not really got it perfect.
Not that it matters but in Objective-C/SpriteKit I would manually do it this way:
//////SAVE THE REALATIVE POSITION BETWEEN THE NODE ON SCREEN AND IN THE CONTAINER//////
[_relativePositionDifferenceArray addObject:aNode.name];
[_relativePositionDifferenceArray addObject:[NSNumber valueWithCGPoint:CGPointMake(aNode.position.x - _containerNode.position.x, aNode.position.y - _containerNode.position.y)]];
[_relativePositionDifferenceArray addObject:[NSNumber numberWithFloat:aNode.zRotation]];