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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Moving a Child to Location

Discussion in 'Scripting' started by t-heo, Dec 5, 2019.

  1. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    Dear Unity Community,

    I would need help with the following issue:

    I have a parent with child components. I would need to move its child component to a precise location. However, whenever I move them, they either stay in their position or move to a wrong one (depending if I use transform.position or transform.localPosition). Also, when I query their location, all child components return the location of the parent. In the Inspector, all the child component locations show 0, 0, 0.
    I also tried Instantiate, with no better results, any child just stays in its current location.

    How could I move a child component then reliably to a location?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    Show your code, otherwise we have no way to know what's wrong with it.
     
  3. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    Hi, sorry for not doing this in the first place:

    examinePos = playerTarget.transform.localToWorldMatrix;
    Debug.Log(foundItem.transform.localToWorldMatrix);
    Instantiate(foundItem, examinePos, foundItem.transform.rotation);

    I would like the child (foundItem) to get moved before the player which is the examinePos. With this script (or by using transform.positions), the child is always moved to a wrong location. Also, in inspector, the Transform of the children is always 0, 0, 0 regardless where they are.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    It looks like you may be misunderstanding what worldToLocalMatrix does. In general it's very rare that you'll actually need to use that, so unless you're doing very precise and relatively advanced math with it it's probably not the right solution.

    So with this in mind, I'll assume that you also tried using transform.position exactly where you have localToWorldMatrix here. First thing that comes to mind is that you say you would like the child "moved", but you are calling Instantiate, which creates a copy of the child at the specified position. If you're looking at the original child when that happens, it would certainly appear as if it's not moving. Could that be it?

    Try
    foundItem.transform.position = playerTarget.transform.position;
     
  5. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    Dear StarManta, thank you for your continued help.

    Unfortunately, that is exactly the code that I tried right at the start, with unsatisfactory results. When this is used, results are strange; a child item is placed way off the playerTarget. However, if I attach a new object as a child and this is set as foundItem, it moves to the exact location. However "native" child objects (child components that are part of the object as it was imported to editor, not attached later to it) do not, but tend to move to different locations.

    This is issue is related to child behaviour somehow; none of the "native" children have a Transform Position other than 0, 0, 0. Children that are added, however have a Position that matches their accurate world location. A solution would be to make the children have an actual location somehow apart from 0, 0, 0.

    To cut it short; how to get the location of a child that just show's 0, 0, 0 as its Location.
     
    Last edited: Dec 10, 2019
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    There may be something confusing going on in the hierarchy or there may be other scripts affecting the object's position.
     
  7. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    No other scripts affect it, so must be some hierarchy issue. Bad issue.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,360
    Is there any object in your hierarchy that has a weird scale value, like 0,0,0?
     
  9. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    Hi kdgalla,

    No object in hierarchy scale is 0, 0, 0. Scale of all children is 1. The parent is 0,1. It seems when the child objects are moved, they move according to their offset from their parent, hence its impossible to move them to a position as there is no (or I can't find) a way to get a reference to their actual world location or to the offset from the parent.
     
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,360
    In code, transform.position should give you the objects position in world space. transform.localPosition will give you the position relative to the parent (Thats what you see in the inspector).
     
    Joe-Censored likes this.
  11. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    Hi kdgalla, thanks for the post, unfortunately this is what I tried as the first remedy and I only hope it was so easy. With code:

    Code (CSharp):
    1.         Debug.Log("transform position");
    2.         Debug.Log(foundItem.transform.position);
    3.         Debug.Log("transform local position");
    4.         Debug.Log(foundItem.transform.localPosition);


    The debug result is same coordinates for all children, even if they are situated clearly at very different locations. So I am still struggling to get the actual location of the child object; now it just keeps displaying the same XYZ for all. The transform position coordinates are Position of the parent.
     
    Last edited: Dec 19, 2019
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,360
    foundItem.transform.position is the actual location of foundItem in world space. If it's not the position that you are expecting, then maybe foundItem is not the right GameObject. Did you debug.log the name of foundItem?
     
    Joe-Censored likes this.
  13. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Remove it from the parent, move it then parent it again.