- Home /
An easy way to get the global position from a child object
Hello guys, I am making a moving plataform using parenting with the player. In one moment X, I need to get the global position from player to raycast and do some processes. I can use this code for normal objects with rotation(0,0,0):
var playerGlobalPosition = player.transform.TransformDirection( player.transform.position );
But how the player is a moving object, he is always rotating, I don't know why, but this "break" this TransformDirection function, changing the X axis with Z axis and etc when calling it. Is there a easier way to get the global position from a child prefab, this away sux. There should exist a function that make this better. If there is not, how can I get the global position without the influences of the rotation?
Thanks for the attention.
Answer by Bunny83 · Mar 17, 2012 at 03:26 AM
It seems you're a bit confused about what you're actually doing in your code up there.
Transform.position already returns the objects world-space position.
Also Transform.TransformDirection can only be used for direction vectors since this function just applies the rotation (and scaling AFAIK) but no offset.
The function you might have thought about is Transform.TransformPoint which transforms a local point into worldspace. The objects position would be:
transform.TransformPoint(Vector3.zero);
// which is the same as:
transform.position
The local position in relation to the parent object is transform.localPosition.
If an object doesn't have a parent .localPosition and .position will return the same value.
Oh, you are right, I managed to get the global locations both with the position and the function TransformPoint, thank you:
var playerGlobalPosition = movingPlataform.transform.TransformPoint( player.transform.localPosition ); //function way var playerGlobalPosition = player.transform.position; //normal way
"Transform.position already returns the objects world-space position" No, sorry, not always... Unity 's doc very often far way from the truth. In my case, child transform.position returns the PARENT world position, not the CHILD's position.
I agree with andyrev, currently I have 10 generations of parents connected by joints in my rope. When I try to move the bottom one it can go waaaay beyond 1 but it's position is 1.396984E-09 ... %-) suddenly it can jump to -1.396984E-09 when its at the bounds of the joint without even noticeable change in position
Uhm, you don't seem to understand scientific notation of numbers. A value of 1.396E-09 is actually 0.000000001396 and -1.396E-09 is -0.000000001396. So they are both essentially zero.
And no, Transform.position always represents the worldspace position of that object. Show me any example where that's not the case. What andyrev said is just not true and he probably based his conclusion on a false interpretation of some results he got.
Nested gameobject in Unity form a kinematic chain. Every object only has a local position, a local rotation and a local scale value. Those values are always relative to the immediate parent. If an object doesn't have a parent it is equal to it's world space position / rotation / scale.
That means a child transform has the world space position calculated like this:
child.parent.TransformPosition(child.localPosition)
this should give "pretty much" the same result as reading
child.position.
The 3 local parameters (localPosition, localRotation, localScale) form a local transformation matrix which transforms from local space to parent space. Combining all matrices up the hierarchy will essentially give you the object's localToWorld matrix
Ah, the reason they're most likely all returning the same is that the centre of the object is actually at 0,0,0 but the mesh is displaced. So Unity doesn't return the meshes location but the position of the object. So you'd need to go into your 3D editing software and set all pivots to the centre of the objects. Its stupid how it does that but it's just how it is
For nested objects, transform.position ABSOLUTELY gets the parent position.
Answer by puppeteer · Mar 17, 2012 at 03:23 AM
I may be missing something here, but isn't transform.position the actual global position in world space?
http://unity3d.com/support/documentation/ScriptReference/Transform-position.html
According to this if you use transform.position you'll get the global position of the object even if it's a child of another.
BTW, transform.localPosition gives the local position relative to the parent
Answer by KrisTUD · Jun 26, 2020 at 03:57 PM
I'm quite sure transform.position returns position in relation to parent not the world I was gonna include screenshot as proof but the forum throws parsing error and doesn't even allow me to link it as a photo either :/ so available here: https://drive.google.com/file/d/18XJPemNF0eRBfl6vUbL4E_IiuTyLCwMc/view?usp=sharing
[code]
void Awake() { ropeBottom = transform.GetChild(transform.childCount-1).transform; }
void FixedUpdate() {
Debug.Log(ropeBottom.position.x);
... [/code]
The world position there is roughly around .6 but console logs -1.396984E-09
I'd have to agree that it isn't always giving global position.
Example I'm dealing with:
Take a quad, put it at global position 0,0,0
add a child object
Rotate the empty in one axis, say around X, for any angle
position the child object into the direction of the rotation, i.e. Z axis with localPosition of (0,0,1)
when you look at transform.position of the child object it will NOT give you a value in the Y axis even though the child object has clearly changed it's altitude.
Would really like to figure out how to deter$$anonymous$$g the real global pos of the child
Answer by ristophonics · Apr 12, 2017 at 05:30 AM
Similar or Same problem. I have an empty parent at (0,0,0). The parent has child gameobjects dispersed around the scene that are not at (0,0,0). The child transform(s) show (0,0,0) which I believe is their local position, relative to the parent. When I query their actual global position Unity (transform.postion) seems to be combining the parent transform and the child localposition ending up with (0,0,0) and not the specific location of the child. Cant find the obvious solution either.
For reference I am trying to instantiate prefab gameobjects at the position of the child gameobjects. Help =)
If you see (0,0,0) on the child object, then it is most definitely not dispersed like you think. It's sitting exactly at the same position as the parent.
The reason for this could be that the pivot point of the child object ( is it a 3d mesh? ) is not at its center, meaning that the actual mesh pivot is someplace else.
Answer by vigneshbca1215 · Jul 31, 2021 at 05:00 PM
Actually @Bunny83 is correct!
The problem is, this doesn't work inside the child's script. transform.position inside the child object's script returns local position. To get the position of the child's position relative to the world space, you gotta access it from the parent object's script like so,
//Inside parent's script returns child's position relative to world space transform.GetChild(0).position;
Your answer
Follow this Question
Related Questions
transform.position on basis of rotation 0 Answers
Align objects based on child objects 0 Answers
Ragdoll breaks and flys far into the air... 0 Answers
Cannot change camera position (2D) 1 Answer