Search Unity

Slaving a Parent Position to it's Child

Discussion in 'Scripting' started by steveauch, Sep 16, 2016.

  1. steveauch

    steveauch

    Joined:
    Jul 11, 2016
    Posts:
    5
    To solve this problem: http://forum.unity3d.com/threads/se...nipulate-a-public-vector.431376/#post-2789586

    I decided to have two objects (spheres in this instance, but it doesn't really matter), as children of an object I want to be able to manipulate in the Scene editor. Thee idea being, that if I move the objects around, their parent can then be notified, query their positions, and move itself around accordingly. But I'm getting very strange relative positioning when trying to set the parents position to that of the child.
    The code below has just one point, which modifies the centre of the object to test the theory. P0 is the child object, and has a script which calls back reposPoints() when the child position is changed. It almost works. The parent object follows the child, but at half the distance. If I uncomment the last line, it follows, but "resets" to zero at each mouse-down.

    Any explanation for these behaviours? I'm stumped.

    thanks in advance.

    [ExecuteInEditMode]
    public class RoadPlanePoint : MonoBehaviour {

    public GameObject P0;

    public void reposPoints()
    {
    Debug.Log("reposPoint " + P0.name+" "+P0.transform.position + "local: " + P0.transform.localPosition);
    Debug.Log("reposPoint " + this.name + " " + this.transform.position + "local: " + this.transform.localPosition);

    this.transform.position = P0.transform.localPosition;


    //P0.transform.localPosition = Vector3.zero;
    }
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    When you move the child, and forwards its localposition to the parent position the child moves - but - you're holding the gizmo so the editor moves it back, and it moves back the parent etc.

    Try setting the parent position to the child world position, and right after set back the child world position to the parent position this way you don't have the editor fighting the child's position with your script.
     
  3. steveauch

    steveauch

    Joined:
    Jul 11, 2016
    Posts:
    5
    Hi Met44,
    thanks for that. I'd been there. I tried it again just to be sure; I presume this is what you meant:
    this.transform.position = P0.transform.position;
    P0.transform.position = this.transform.position;

    Really weird stuff starts happening then, and the whole object gets into some sort of a weird feedback loop, and the object zooms away at the speed of light.
    I think there is some setting and resetting of relative and world positions that happens in the background (somewhere sometime) and messes with it all.
     
  4. steveauch

    steveauch

    Joined:
    Jul 11, 2016
    Posts:
    5
    I've given up trying to figure out why it didn't work, and just decided to make them siblings under a common parent. Now it works fine. I wanted to avoid a "find" by having the parent-child relationship, but it's too much hassle.