Parent Game Object to Follow Child - unity3d

Is there a way to have a parent object follow the combined bounds box of the child objects?
I have child objects which I apply force to for movement (via `AddForceAtPosition). The problem is that they leave the parent behind in world space. I understand that children are relative to the parents position. But is there anything I can do to address this?
I'd really like to keep to keep the force being applied to the child object's rigid bodies as it gives me the physics behaviour I need.
I have experimented with getting the bounds of all child objects, then setting the transform of the parent to the children. But this causes jittery behaviour since moving the parent also moves the children.

There are two ways of doing this, the best way is to change the structure following:
A (parent)
B (child 1)
C (child 2)
to:
Root (new parent with nothing on it)
A (old parent)
B (old child 1)
C (old child 2)
Then move A the way you tried before. You might also want to look into Joints, and see if this might suit your need better. If you don't want two colliders to interact with each other, you might also want to look into Layer Based Collision Detection and Physics.IgnoreCollision().
An alternative that I really wouldn't recommend, is counteracting the movement of the parent with the children. So whenever you move the parent you move the local position of the child you want to stay still the opposite amount. This however could possibly make your physics wonky.

Related

Need to SCNNode within restricted area

I have created a SCNNode object on a SCNPlane object. Both objects are added to the scene view root node, like below:
scView.scene.rootNode.addChildNode(ballNode)
scView.scene.rootNode.addChildNode(planeNode)
Now, my issue is that the ball node is moving out of the plane node boundary.
When I tried to add ballNode as planeNode's child then, the ball node is not getting added to a plane as expected. It behaves very strangely.
Basically, I need to restrict the movement of ballNode within planeNode area. How can I do it?
Great question!
When adding a child node to another node, there are no constraints being implicitly applied to the child, aside from existing within the infinite coordinate space of the parent node.
While changing the transform (eg position) of the parent node will directly apply to all child nodes, each child node can still freely transform within the parent nodes local coordinate space.
It'd be a good idea to read the SCNNode docs if you haven't already, as it covers the foundational aspects.
To do what you want, when moving the ball in your gameloop, you will need to manually check whether or not that ball is outside the boundary you are trying to enforce, and change its position accordingly.
Depending on your desired outcome, you may want to also check out SCNPhysicsBody as a way to construct your scene with implied physical boundaries.

Move a GameObject by its child objects

I wanted to create some evolution like "game". I make some bones, muscles, etc. When I hit play the creature moved but when I've checked the distance it stayed 0. What I've discovered was that all individual child elements moved but the containing GameObject was stil at the same position.
What is the (correct) way to make changes of a child object affect the parent object? Like moving legs to move the person.
The question seemed clear in my head but after reading your questions, remarks and comments I needed elaborate more:
I understand the basic idea of walking animations and such (i think). But here I'm trying to move a creature by itself. At first purely random movement and hopefully later on via a neural network.
In the sample screenshot below you see a creature, the spheres are colored for its weight and the beams between the spheres are the bone/muscle. And my creature limbs do move by pulsating the bone/muscles... but the position of the parent object stay the same. The magenta line is the position of the parent. I would like that the position of the parent would follow the center of its elements.
Your question is a bit unclear to what exactly it is you are after. However, if you want the child-parent behaviour become reveresed in a way that child becomes parent and parent becomes child just for function A() , you can simply swap the child and parents relation in A() itself,
As an example,
void A()
{
child.transform.SetParent(parent.transform.parent);
parent.transform.SetParent(child.transform);
... // your A function
parent.transform.SetParent(child.transform.parent);
child.transform.SetParent(parent.transfotm);
}

Unity Gameobject is far away from the Move Tool

why is my Gameobject so far away from the Move Tool as you can see in the Picture down below?
I would like to have it centered at the Gameobject. How can I change this?
GameObject away from Move Tool
Screenshot of Hierarchy
Check the object in the scene hierarchy and make sure that the transform eg. x,y,z are set to 0.
If your object is a child of another object and they are not at the same position you are probably moving the parent and not the child object itself.
that Move Tool is the objects origin, and this might help:
"The easiest way is to create an empty object as a parent object. Then you put the corridor object as a child of the empty object. You can then alter the position of the corridor object so its positioned at the edge.
i found your answer here
note that its not exact, in your case, make an empty object, move it to where you would like your origin or move tool THEN add your other object to it as a child. you can then move it with the parents move tool(origin) right on top of your box.

Swift SKSpriteNode: Complex Sprite Textures?

This is a question regarding best-practice for implementation. For an example, I will reference a simple game called Pixel Claw.
Suppose I had an SKSpriteNode akin to the claw from Pixel Claw, in that what the SKSpriteNode might be ''holding'' is variable (but finite in possibilities, e.g. four different objects).
What the node SKSpriteNode is holding has no agency of itself.
Thus my question is: is it better to use different textures such as a claw, a claw holding object a, a claw holding object b, etc or have two SKSpriteNodes and position the SKSpriteNode with the claw texture to be next to the SKSpriteNode with an object texture?
I am not making a claw game, it was just the first example that popped to my head where both could be plausible solutions. The former being more simplistic - just switch the texture, the latter being more generalizable.
If the latter is the best solution all around, how can one ''pair'' the movements of the two sprites?
I would use the second approach especially if the objects a and b can fall out of the claw at some point.
You should make the texture of a claw, the texture of object a, and the texture of object b.
To make the objects move in sync with the claw, simply add the object as a child of the claw node. This way, moving the claw will cause the object to move as well!
Note that before adding the object as a child, you need to make sure that the object does not have a parent. If it does, you must remove it from its parent before adding it as a child of the claw. The same thing applies when you "release" the object from the claw: you need to first remove the object from its parent (the claw) and then add it as a child of the desired parent.

unity parent relative to child position and rotation

I've looked around for some ideas on this to no avail. I have a scene in unity where a parent has four children positioned and rotated in relation to the parent (though not necessarily the same position and rotation as the parent). After moving and rotating the parent, the children should follow the parent, and reset their position and rotation in locally similar way to parent as before, preserving a similar formation. In particular, I can get the children to follow the parent, but once the parent reaches their destination, I don't know how to reset the children to their appropriate formation positions and rotations once the goal is reached. I'm not looking for someone to answer my question as much as a headstart on how to make this happen. Any help is greatly appreciated. Thanks everyone.
Assuming you have a script on your parent,
One approach will be to store the Transforms of the Children before the Parent starts to move and after reaching the destination, apply the Transform back to the children :)
[Child's transform is relative to the parent, so the child will be displaced if the parent is rotated since child is not at (0,0,0) relative to the parent]