Unity3D - What does child object inherit from its parent? - unity3d

What does child object inherit from its parent in Unity except transform (when parent object is moved, then children are being moved too)?

Unity's inheritance isn't like pure OOP inheritance. It's not like a base class provides virtual members that a child inherits.
In Unity a child object inherits only the Transform. It doesn't really inherit it, it just becomes the base for it's own Transform component therefore when modifying the child component it will be relative to the parent. Since all object by default need to at least have only 1 component (Transform) that's pretty much all that can be inherited by a child object.

The child will not inherit the parents transform directly, rather the childs transform becomes relative to that of the parent. So if we take the example of the transforms position, a GameObject without a parent will be relative to the world coordinates, whereas a GameObject with a parent will be relative to the parents position. You can get the relative position using localPosition, which will be equal to the Transform.position if the GameObject has no parents.
As a more concrete example (I'll use 2D coordinates for simplicity):
Say we have a GameObject (A) at world position (0,0) without any parents. Its Transform.position will be (0,0) and Transform.localPosition will also be (0,0).
If we add another GameObject (B), make it a child of A and set its world position to be (1,0) then its Transform.position and Transform.localPosition will both be (1,0).
Now if we were to move GameObject A to (2,0), we would see that B would move to (3,0) in world space, but its Transform.localPosition would still be (1,0) as this is relative to the position of A.
The child won't inherit anything else from the parent, though the relationship can be used in code to obtain references to each other via Transform.parent and Transform.GetChild.

Related

Parent Game Object to Follow Child

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.

Some problem with rotation angles in unity?

I just attached one object to another. I don't understand what happened, but the rotation angles of the child object in the inspector or myChildObject.transform.rotation.eulerAngles (or myChildObject.transform.localRotation.eulerAngles) are different.
The Inspector shows the localRotation of objects, which will not change unless you change (or reparent) that specific object. Changing a parent's rotation or localRotation will never change its (existing) child's localRotation.
A child object with 0,0,0 Euler angles just means it isn't rotated compared to its parent -- i.e. its world rotation should match its parent's world rotation.
Rotating the parent should change the child's global rotation, but if the child does not have a localRotation of 0,0,0 they won't match. For example if the parent had a rotation of 45,0,0 and the child had a localRotation of 0,0,90, the child would end up with a global rotation of 45,0,90. (Things can get much weirder than that, though.)
To put it another way, the child's global rotation is its own localRotation combined with the rotation of its parent/s.

How to get the absolute position of a child entity in bevy?

I am using the Bevy game engine.
The ability to have transforms be propagated to children in Bevy is handy, but when I am performing collision checks in my game, I have been using the object's Translation to compute its location. Now that I have some parent-child hierarchies in my scene, the Translation of each child entity is relative to its parent.
Is there a way to get the position of an entity relative to the world origin as opposed to the entity's parent?
The "world" position is stored in the GlobalTransform component. Transforms internally are 4x4 matrices where the translation() function returns the position. You can access it like this:
fn system(global_transform: &GlobalTransform) {
let position = global_transform.translation();
}

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.

Weird X and Y of gameobject in Unity

I have one problem. X and Y of gameobject are weird. I think that's because its parent isn't main camera. How to set parent without scripting?
To make a gameObject the child of another, look at your hierarchy and find the gameObject you want to make child, drag and drop it on the other object you want to make parent.
This will make the coordinates relative to the parent.