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

Drop an object and making its position relative to parent, how to?

Discussion in 'Scripting' started by Suminsky, May 18, 2012.

  1. Suminsky

    Suminsky

    Joined:
    Aug 11, 2011
    Posts:
    50
    I have a bunch of npcs;
    Now I want to add a sprite shadow to all of them;
    The shadow will always be at the same position RELATIVE to the npc;

    BUT, even setting the shadow to be at the correct relative position, when I drop it as a child of an npc, ON INSPECTOR, unity changes the shadow position, to make it keep its world position..

    How can I change this behavior? I wanna keep the position, so I can just drag and drop the shadow to my npcs..

    Probably something simple I dont know
     
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    I am not sure what npcs is in this context.
    But I don't think there is a setting for that. Keeping the world position when parenting an object makes more sense.
    You can have a script (or an editor script) that sets all you shadows positions to the desired local position.
     
  3. Suminsky

    Suminsky

    Joined:
    Aug 11, 2011
    Posts:
    50
    My npcs are just game objects..

    The functionality I want is just to make unity to NOT update the object trafo when dropping it into another object (when making it a child)..

    Im not sure if its clear, to me seems something simple, and I believe (not sure) is what unity does when you do that by script.
     
    Last edited: May 18, 2012
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    There are basically two ways to achieve this:

    a) make the NPC's transform the parent of the shadow's transform. Easily achievable via Shadow.transform.parent = NPC.transform (just as an example). In this case you'll only need to set the shadow's position once, either before making it a child using the world coordinates (transform.position), or after making it a child using the local coordinates (transform.localPosition)

    b) Create a script which updates the shadow's position every LateUpdate(). Reference the corresponding NPC in that script and set the position in relation to that one.

    While a) is surely more hassle free and will come in handy later when you want to move the whole NPC around or so, it has one huge disadvantage: The parenting will most likely break your shadow's draw call batching. Meaning if you have 20 shadows on screen, each will produce a draw call (= 20 draw calls). Assuming you want to batch the draw calls and every other precondition for this is met, you will want to stick to b).
     
    Last edited: May 18, 2012
  5. Suminsky

    Suminsky

    Joined:
    Aug 11, 2011
    Posts:
    50
    you mean Shadow.transform.parent = NPC.transform, am I right?
     
  6. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Whops, sorry. Yes, as you want to parent the shadow to the NPC it needs to be the way you mentioned. Edited in the post above.