- Home /
Altering object's position without changing its childrens position?
A parent object needs to be moved without having its children changed. Anyway to do so without changing every single childrens position everytime the parent moves?
Why?
The parent-child relationship is primarily to keep positions relative. If you don't want this movement, then you probably don't want a parent-child relationship
I have parent-child relationship for easier scripting... I'm expecting more than 1000 units to be generated and divided into squads, thus having them laying all around makes no sense since there would be extra useless calculations as to maintain the squads consistency. Ever played Total War games? If you did, you may get the idea though I'm reducing squad size but still, if every single unit had to calculate (find) its relative distance to hostile object in the scene, I doubt I would be making a mobile game. Furthermore, I wonder how heavy the game would be with Unity Engine being a behemoth base to run on...
Basically I need to move parent but not children position is due to the fact that
1) I'm making a squad based rts (warhammer 40k, Total War type.. You get the idea)
2) $$anonymous$$obile rigs are very heavy, with Android running heavily on background and using Unity to make the game, it is heavy enough that I dont want extra O(n^2) case per frame to check and set target. (Atleast individual units should not move around even though it has nothing to do meaning that I need to check other members of the squad to see if they can move on...) while having extra n cases whereas having triggers per unit to check valid attack range. I doubt I would get 60 on PC and lucky if I get 20 on mobiles...
3) the game I'm trying to make is a replica of another game ( not TW or W40k, just some weird game) which I'm guessing that has both collider to check if one squad collides with other squads and moves based on the coord from searching other squad to attack. (yes, this causes weird issues of units going to nowhere because that was where its target was and I'm aware and I want to make it so)
I can calculate a position of the squad using coord of children units rather than actually moving parent object so that the game works well but I was hoping for a better solution like actually moving parent object in the direction synchronized with children rigid body force so that I can get a reliable yet quick reference to squad position.
Answer by Andres-Fernandez · Aug 20, 2014 at 07:47 AM
Temporarily reparent children to another object.
Move parent
Reparent children to parent
What happens if the parent moves via addforce or just every fixedupdate? I dont think it is wise to do so..
If you want to move parent but not children, why have the children parented to the parent? If it's just for some code like BroadCast$$anonymous$$essage, you can just create references to the children in the parent's code and send them messages.
[edit] If parent moves within fixed update reparenting would work... Not with the addforce tho...
Basically I have a squad of units. I can't make a single unit to be its leader as it can die(destroyed object) so I need to keep them controlled under an empty object for easy retaining and generation as well as other stuff; in this case I rely on the squad object position to set as target. Don't ask me why I chose this kind of movement options rather than just moving towards another squad while checking targets coordinates. I happen to be trying to make a full replica of another game.
Ins$$anonymous$$d of a child/parent hierarchy, you might have it easier with a simple custom class that stores a leader gameObject and a list for the squaddies, and then use these references to command units.
Answer by Cherno · Aug 20, 2014 at 01:50 PM
While the parent object is moving, you also make all children objects Vector3.Lerp towards the original position, which you saved before moving in a Vector3 variable.
Answer by joergzdarsky · May 28, 2016 at 09:17 AM
I'd like to add a request for this possibility (transform / position parents but not the children).
Here is my usecase: I am using the (commonly used) "Floating Origin" technique for my space game. So, I generally keep my camera close to the origin (0,0,0) for precision reasons. When the camera has moved to a certain distance, I reset the camera to the origin (0,0,0) and move all objects around for the same distance -> Floating Origin.
In that case I have to move a lot of object at once. Extreme situations where I have to move everything each frame is not unlikely. Thus a highly optimized strategy is required for performance reasons.
The way I do it now is to traverse through the children of an object (which is an empty "Space"-Object) and move each object individually by the same distance. The corresponding script (which realized Floating Origin) is attached to the camera:
[RequireComponent(typeof(Camera))]
public class FloatingOriginByParent : MonoBehaviour
{
public float threshold = 6000.0f;
public GameObject parentGameObject; // GameObject where all child objects are to be shifted
void LateUpdate()
{
Vector3 cameraPosition = gameObject.transform.position;
if (cameraPosition.magnitude > threshold)
{
foreach (Transform child in parentGameObject.transform)
{
child.transform.position -= cameraPosition;
}
gameObject.transform.position = new Vector3(0,0,0);
}
}
}
Thats the best you can do in Unity AFAIK however its still not optimal.
The above results in thousands of single shifts including a large loop, each frame. It would be performance-wise much better if I could group all objects under a "Drag" or "Anchor"-like empty gameobject, that is once shifted (including the children) for the required distance, and then reset to the origin (without the children). Doing that manually is even worse than the above code. However, shifting lots of objects by its parent is faster, but to realize Floating Origin with that you need a function to independently reset the parent.
So an additional function to transform position a parent gameobject without the children would be the key here.