Search Unity

Moving a parent when child moves

Discussion in 'Scripting' started by lukasaurus, Aug 31, 2014.

  1. lukasaurus

    lukasaurus

    Joined:
    Aug 21, 2011
    Posts:
    360
    I have these rooms that are modular and linked via "exits" (empty gameObjects with their positive x-axis aligned in the direction that the room exits and begins a new one.

    Starting with an initial room, I am doing a breadth first expansion. I've got that part working. It instantiates new objects up to the maximum number of rooms specified.

    However, my next step is to align the exits of one room with the exits of the new room. The "exits" are children of the room gameObject, and I want the parent object to rotate and reposition as well as the child object.

    Code (csharp):
    1.  
    2. function AlignNewRoom(oldExit, piece){ //exit is old exit, piece is new room
    3.     var newExits = FindChildExits(piece); //get all exits (randomised)
    4.     var newExit = newExits.Shift(); //shift off the first exit (this will also remove it from the list and won't be considered for further expansion
    5.         //pseudocode
    6.        newExit.rotation.y = reverse oldExit.rotation.y  //reverse the rotation of the old exit
    7.        newExit.position = oldExit.position //position the exits so they overlap
    8.  
    9.    
    10. }
    11.  
    How I so so the part labelled pseudocode?
     
  2. lukasaurus

    lukasaurus

    Joined:
    Aug 21, 2011
    Posts:
    360
    nevermind, figured it out. Got the rotation of the newExit, old Exit, figured out the difference, rotated the parent of the new instance that much. Then figured out how far and where to move the parent object
     
  3. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    First calculate rotation, and after calculate relative position between initial room and parent-child difference. This will be the parent position.

    Important, all exits must look at (forward axis) to center room, to calculate rotation correctly.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class piece : MonoBehaviour {
    4.  
    5.     public Transform startDoor;
    6.  
    7.     Transform[] exits;
    8.     int exitSelected = 0;
    9.     int oldExitSelected = -1;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.      
    14.         exits = new Transform[transform.childCount];
    15.         for(int i=0;i<exits.Length;i++) exits[i]=transform.GetChild(i);
    16.  
    17.     }
    18.  
    19.     //Last range not included
    20.     int intRepeat(int value,int range){ return (value < 0) ? range + value % range : value % range; }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.      
    25.         if(Input.GetKeyDown(KeyCode.RightArrow)) exitSelected = intRepeat(exitSelected+1,exits.Length);
    26.         if(Input.GetKeyDown(KeyCode.LeftArrow))  exitSelected = intRepeat(exitSelected-1,exits.Length);
    27.  
    28.         if(oldExitSelected != exitSelected) selectExit();
    29.      
    30.     }
    31.  
    32.     void selectExit(){
    33.  
    34.         oldExitSelected = exitSelected;
    35.  
    36.         //Rotate first
    37.         transform.rotation = startDoor.rotation;
    38.         transform.rotation *= transform.rotation * Quaternion.Inverse(exits[exitSelected].rotation);
    39.  
    40.         //After, calculate position;
    41.         transform.position = startDoor.position + (transform.position - exits[exitSelected].position);
    42.  
    43.     }
    44.  
    45. }
    Download project link.