Search Unity

Can you make a child in prefab not take the parent transform?

Discussion in 'Scripting' started by BobFinery, Oct 31, 2014.

  1. BobFinery

    BobFinery

    Joined:
    Oct 14, 2014
    Posts:
    46
    Hi,

    If I have a prefab containing a child can I make it so the child doesn't take and translation/rotations applied to the parent in game?

    I have an object that I want to indicate to the player that it can be picked up. So, I have a cube (the object) and above it is an arrow. I want to make the cube spin but doing that makes the arrow spin around as well, I want that to remain in the original position it is setup in the prefab.

    Cheers,
    Bob
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    You could adjust the object hierarchy and then rotate the inner one:
    Code (csharp):
    1. Health Pack
    2.   |-- Mesh  (spin me instead of root object)
    3.   |-- Arrow
    another example:
    Code (csharp):
    1. Health Pack
    2.   |-- Container  (spin me instead of root object)
    3.         |-- Mesh
    4.         |-- Something Else
    5.   |-- Arrow
     
  3. BobFinery

    BobFinery

    Joined:
    Oct 14, 2014
    Posts:
    46
    I have found a way to do this by doing the following:

    In the parents Update function

    find the childs transform and setting its parent to null

    continue to modify the parents transform in the update function as required
    at the end of the update set the childs transform.parent back to the parents transform

    Seems to work well
     
  4. BobFinery

    BobFinery

    Joined:
    Oct 14, 2014
    Posts:
    46
    cheers numberkruncher, I found a way that is easy to do

    I think your "adjust the object hierarchy" is what I am doing by removing the parent from the childs transform temporarily
     
  5. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Much easier to do what numberkruncher suggested. No need to unparent and reparent every frame. Seems like a hassle.
     
  6. BobFinery

    BobFinery

    Joined:
    Oct 14, 2014
    Posts:
    46
    I remade my prefab with the hierarch numberkruncher suggested. It was a bit more work to do it that way but in the end I didn't like switching the parent on the fly as it did seem to be going against the grain somewhat. Cheers for the help