How to stop an object from following its parent?
I have an enemy that's patrolling an area. That area is defined by two gameobjects that are just transform positions, one called 'start' and another called 'end'. Both are children of the enemy, that way, every time I create a new enemy, they each have their own 'start' and 'end'. It just looks a lot cleaner in the hierarchy. However, they both follow their parent, which they're not suppose to do. Is they're any workaround for this?
Well, you could add code (probably to LateUpdate) that resets those GameObjects' positions every frame. But to me it looks like you don't want those transforms to be parented to the turtle.
If it's really important to you that each turtle is grouped with its start and end transforms in the hierarchy, you could put the 3 objects (turtle, start, end) at the same level, inside a bespoke container object. That way the 3 things would move independently while still having them grouped.
To rephrase, make empty "turtleHolder" with children "turtle," "start" and "end." I've successfully used that trick. It's a cheap way to spawn, move and destroy them all at once. And almost as easy to find your start&end (parent.Find("start").)
Probably the dictionary definition. When you get to a device with internet access you can look it up :-)
Can we please get a way to preserve our hierarchical structure and decouple parent-children from movement? it's really useful when objects stay associated even when they move away from each other. I know you can do it with a wrapper and either use a mediating script in the parent, or else traverse the hierarchy to it's named sibling or script name, but those are all very messy. I want to be able to just 'drop' things on the ground that belong to the parent entity sometimes. Persistent AOE for instance, or really any collision based attack where the velocity is not high enough for the movement of it's origin to be irrelevant.
Not quite sure what you're asking for here, but for exposure I suggest it would be better to post a new question for it rather than add the as an answer to this question.
The way I see it, the hierarchy view is just a view of the scene/prefab showing the parent/child relationships, a child being an object that moves with its parent.
If you want to create other kinds of relationships between objects you can, but they'll be the specific relationships that you need (you say "stay associated" as if it's obvious what that means, but it isn't) - so it seems reasonable for you to have to implement them yourself.
Answer by NinjaISV · Dec 03, 2017 at 09:36 PM
You could either set the children's parents to null as soon as the game starts (this would be the best on performance, and it will still look clean and organized while in edit mode).
// Make sure you assign the children in the inspector here.
public Transform[] children;
private void Awake () {
// Sets each child's parent to null while keeping their world position.
foreach (Transform child in children) {
child.SetParent(null, true);
}
}
Or you could store their positions in a list and constantly update them in FixedUpdate. (Note: this is VERY bad on performance as opposed to the other method due to the constant matrix calculations.)
// Make sure you assign the children in the inspector here.
public Transform[] children;
private List<Vector3> childrenPositions = new List<Vector3>();
private void Awake () {
// Sets each child's parent to null while keeping their world position.
foreach (Transform child in children) {
childrenPositions.Add(child.position);
}
}
private void FixedUpdate () {
for (int i = 0; i < children.Length; i++ ) {
// Ensures that the child is not null and that the index is not out of bounds.
if (children[i] != null && i < childrenPositions.Count) {
children[i].position = childrenPositions[i];
}
}
}
And lastly, you could save the world positions at the start of the game, and reference those:
// Make sure you assign the points in the inspector here.
public Transform start;
public Transform end;
private Vector3 startPosition;
private Vector3 endPosition;
private void Awake () {
// Assigns the start and end points.
startPosition = start.position;
endPosition = end.position;
// Destroys the start and end Transforms from the game to save on performance.
Destroy(start);
Destroy(end);
}
Hope this helps!
Answer by mefsh · Dec 02, 2017 at 11:52 PM
Not that I know of.
You could instead store the start and end positions as Vector3s in the script. https://docs.unity3d.com/ScriptReference/Vector3.html
Answer by celinedrules · Dec 12, 2021 at 04:13 PM
You could add a rigidbody to it and set its body type to static.
Your answer
Follow this Question
Related Questions
"Invalid argument"? 0 Answers
Accesing The Script Under GameObject 0 Answers
Unity Sprite renderer bug?? 0 Answers
Does anyone know how to make an object(enemy) follow a path set by the player in a 2D side scroller? 1 Answer
How do you Isolate Physics2D.OverlapArea to a single script/gameObject/Instance? 0 Answers