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.
Related
How to rotate a body relative to given dx dy in Unity? For example, I have a weapon sprite with a hand, and I was able to make it rotate relative to its center so that the weapon looks at the cursor, how can I rotate not relative to the center of the sprite, but relative to a given point?
The simplest way to achieve this is to set a gameobject at your desired point and make your object child of the created object. So when you rotate the parent, it will rotate relative to the parent point.
You can create an empty object and set the body as its child, then rotate the parent, the body will rotate around the parent like the parent be its pivot.
This is a practical way to set the pivot, and you can change it at any time.
I am working with Matrix4x4's in Unity and have some local space Matrices under parent Transforms that have their own rotation, scale and position. Whenever a parent Transform is moved, scaled or rotated I need to reflect it also on to the child Matrices.
For translating the position of the child into worldspace relative to the parent, that seems pretty easy:
WorldSpaceMatrix = LocalSpaceMatrix * transform.localToWorldMatrix;
However I am really struggling with how to reflect a change in parent rotation on to a child Matrix and also update position due to any position offset. The above gets the local rotation of the Matrix itself which is fine. But it doesn't update any change in position. Effectively I need to "RotateAround" the parent, but I don't know how.
Is possible to apply a Transform via script in Unity?
What I mean with "applying" is setting the current position/rotation/scale of an object AS their default ones:
if a game object is rotated by 30° on the x axis, the x rotation value on the Inspector will display 30 of course. By applying the rotation the Inspector will now display 0 and the object will keep its rotation. 30° is now the default rotation of that object.
I know you can do this, for example, in Blender, but I don't know if it's possible to do it in Unity as well.
You cannot "apply" it directly to the object, but you can make a parent object, rotate this object when needed and "apply" the rotation or position to the child object, in this case the model.
Your hierachy should look something like this
- Parent (this is the object you want to rotate/position/scale)
- Child (this is the model, you "apply"/set default values to that object)
I have "GameObject 0" (GO0) with an animation controller that used parameters to transform states and is used as a wrapper for "GameObject 1"(GO1). "GameObject 1" has an animation controller that has Root Motion applied. Within GO1 there are 3 game objects whos animation is in the same animation clip as GO1.
This allows me to place GO1 in world coordinates and animate GO1's children in local coordinates to maintain relative positioning.
However, I am wish to move one of the children of GO1 to a world coordinate that is not relative to GO1. I have used script and Vector3.Lerp / Vector3.MoveTowards functions, but they are not working as expected. Is the Root Motion interfering with the Lerps?
I have change the script to add a new parent to the child object and animating the new parent. This seems to work but the rotation of the new parent is causing a new issue.
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.