Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[c#] screen to local/object coordinates, best method

Discussion in 'Scripting' started by pKallv, May 4, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I have a number of objects, using playing cards for test, on top of each other like this: $Skärmavbild 2014-05-04 kl. 00.47.54.png

    The objects are perfectly lined out and then i click on Heart Queen (HQ) and get the following: $after.png

    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:
    Code (csharp):
    1. print ("Here we go - " + MainScript.selectedGameObjectList.Count);
    2.  
    3.         firstRound = true;
    4.  
    5.         // Allocate all selected objects to the container
    6.         foreach(GameObject aGameObject in MainScript.selectedGameObjectList) {
    7.  
    8.             if(firstRound) {
    9.                 // At the first round do not add child as the first object is parent
    10.  
    11.                 theFirstGO = aGameObject;
    12.  
    13. //              theFirstGO.transform.renderer.sortingOrder = 100;
    14.  
    15.                 firstRound = false;
    16.             }
    17.             else {
    18.                 // Add to parent
    19.                 print (aGameObject.name + " / " + aGameObject.transform.position);
    20.  
    21.                 aGameObject.transform.parent = theFirstGO.transform;
    22.                 aGameObject.collider2D.enabled = false;
    23.  
    24.             }
    25.         }
    26.  
    27.         // Make sure the selected container, with all it's objects, get's on top
    28.  
    29.         theMainScript = new MainScript(); // Set up access to MainScript
    30.         GameObject dummyGO;
    31.  
    32.         int lengthOfList = MainScript.selectedGameObjectList.Count;
    33.  
    34.         for(int zz = 0; zz < MainScript.selectedGameObjectList.Count; zz++) {
    35.             MainScript.selectedGameObjectList[zz].transform.renderer.sortingOrder = 100; //transform.renderer.sortingOrder;
    36.         }
    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:
    Code (csharp):
    1. //////SAVE THE REALATIVE POSITION BETWEEN THE NODE ON SCREEN AND IN THE CONTAINER//////
    2.  [_relativePositionDifferenceArray addObject:aNode.name];
    3.  [_relativePositionDifferenceArray addObject:[NSNumber valueWithCGPoint:CGPointMake(aNode.position.x - _containerNode.position.x, aNode.position.y - _containerNode.position.y)]];
    4.  [_relativePositionDifferenceArray addObject:[NSNumber numberWithFloat:aNode.zRotation]];
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you want to only move the parent object, you could try this and adjust/optimize it for your needs.

    Code (csharp):
    1.  
    2. public Transform[] childs;
    3. public bool onlyParent;
    4. void Update()
    5. {
    6.      if (Input.GetKey(KeyCode.W))
    7.      {
    8.           transform.Translate(Vector3.forward * Time.deltaTime*5);
    9.           if (onlyParent)
    10.           {
    11.                foreach (Transform c in childs)
    12.                {
    13.                     // move it into the opposite direction 'back' or  ' - Vector3.forward'
    14.                     c.Translate(Vector3.back * Time.deltaTime*5);
    15.                }
    16.           }
    17.      }
    18.  
    19.      // press C to assign all childs to the array
    20.      if (Input.GetKeyDown(KeyCode.C))
    21.      {
    22.             childs = new Transform[transform.childCount];
    23.             for (int i = 0; i < childs.Length; i++)
    24.           {
    25.                 childs[i] = transform.GetChild(i);
    26.           }
    27.      }
    28. }
    29.  
     
    Last edited: May 4, 2014