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.

5 Answers

5

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 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

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;

Thank you very much!!! That work for me.

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.

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 :confused: so available here: https://drive.google.com/file/d/18XJPemNF0eRBfl6vUbL4E_IiuTyLCwMc/view?usp=sharing


void Awake()    {
        ropeBottom = transform.GetChild(transform.childCount-1).transform;
    }

    void FixedUpdate()    {
            Debug.Log(ropeBottom.position.x);
...

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 determing the real global pos of the child