find out real position of a child GameObject

Hello *,

I have created an empty GameObject in which I have put a couple of child GameObjects.

Now I need to find out the real position of the childs. If I move / rotate the parent GameObject the positions of the childs won’t change.

I have tried:
x = ChildElement*.gameObject.transform.localPosition.x*
and
x = ChildElement*.gameObject.transform.position.x*
but the values are always realtive to the parent GameObject. The parent GameObject can rotate and I need to find out which child is on top while rotating.
Please can you help me?
Thank you,
Ulrich

1 Like

There are a couple of way to do this, and they just aren’t under my skin enough to give you better information, but:

Run through the transform documentation:

and look for things like:

there’s also transform direction/inverse etc.

This might point you in the correct direction. (no pun intended)

That’s not relative to the parent, that’s the world position. Only localPosition is relative to the parent.

–Eric

I’ve never had a problem with it.
transform.position - should be giving the correct World Space position (sum of all the transforms in the hierarchy)

Edit: what Eric said :slight_smile:

I also thought this. But “ChildElement*.gameObject.transform.position.x” will always give the same coordinates while rotationg the parent.*
Now I’m doing:
x = ChildElement*.gameObject.transform.localPosition.x + Parent.gameObject.transform.localPosition.x;*
and this works fine…

1 Like

As stated, Transform.position should always give you the world position.

I don’t have a link, but someone posted a thread just like this a while back. They stated that Transform.position was returning the local position, and several people pointed out that it should in fact always return the world position. It then turned out that there was a problem with the OP’s diagnostic and that Transform.position was in fact functioning correctly.

So, I’d double-check everything and make sure you’re actually printing out (or whatever) what you think you’re printing out. There really should be no need to compute the world position manually like that; Transform.position should work just fine for this.

I’m also having the same problem. Setting the position to (0,0,0) doesn’t work - I have to set it manually in the inspector, as confirmed several times. I’m using the code:
ObjectSelected.transform.position = new Vector3(0,0,0);

transform.position is supposed to set the global position, but it doesn’t always do that. The odd thing is that it was working yesterday, and I didn’t change that part of the code.

Transform.position does always set the global position, no exceptions. Perhaps you have something else moving it (like an animation).

–Eric

1 Like

The game isn’t running. I’m using an editor script.

You should probably look at the scale of the parent as well. That can change the global coordinates of the child too.

I have the same problem, whatever I do it it keeps giving me the transform of the parent, Its driving me nuts.
I have 2 movement fields, the parent moves with a scrolling movement and inside you control the movement of a child object.

Transform.position is the global position of an object, not the parent (if it has one).

–Eric

You all keep saying Transform.position is the global position, but that is not fixing the issue.

I am having the same issue. Transform.position is only returning the local position, not the world position.

EDIT: OK, so the Transform.position DOES return the global/world position.
+My problem is that I was using my code in a method that was not getting called, so I moved my code to the Update method and it worked perfectly!

private void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Player")
        {
            if (!spawned)
            {
                newLoc = transform.parent.position + new Vector3(0, 0, 40);
                spawned = true;
                Instantiate(gameTilePrefabs[Random.Range(0, gameTilePrefabs.Length)], newLoc, Quaternion.identity, parentObject.transform);
            }
        }
    }

    private void Update()
    {
        if (spawnedTile.transform.position.z <= -40)
        {
            Destroy(spawnedTile);
        }
    }

previously, I had

if (spawnedTile.transform.position.z <= -40)
        {
            Destroy(spawnedTile);
        }

inside of the if(!spawned), which was wrong because how would the position ever get updated?

It’s not possible. Transform.position is only the global position, never local. You’re misunderstanding something.

–Eric

That is impossible, show us your code.

edit: Doh, got ninja’d.

Can anyone please explain to my why Unity is such an absolutely unintuitive piece of underdeveloped garbage?
This is not the first time I waste my time using its’ features and assuming they work just like the documentation says just to bring myself to a scalding, raging headache because I discover after days of work that the bug I was searching for in my code is not actually there, but it is some basic feature that does not work as it claims to.

Hierarchy has 4 cubes. One has script attached. Script holds 3 of the cubes. Accessing their position and drawing a ray upwards locally works, as well as from script gameobj, but no matter what I do, nothing (none of the point transform bullshit either) works to draw a simple debug ray between the script gameobj and said cubes. Best I`ve gotten is some random lines from the script gameobj origin into a single direction diagonally as if some kind of multiplied local position of children has been given to it. Local, world, parented, unparented, nested, whatever. Nothing gives a simple. fucking. line. between. two. referenced. objects. in a script. god. why?

Code: Imgur: The magic of the Internet
Output: Imgur: The magic of the Internet

You didn’t bother to understand that DrawRay draws rays. Not lines. Different thing. DrawLine draws lines. Exactly like the docs say. Nice unhinged rant though. Take responsibility next time instead of blaming tools for your own lack of understanding. Not to mention your post was an off-topic necro.

–Eric

2 Likes

Thank you for pointing that one out.

BUT YOU ARE USING transform.parent.position not children :S So I believe this is totally indeed

im having an issue when detecing heigh fall of y position when the character enter into a kind of elevator, he become to be children of it so it moves along y axis, then if i fall just moving away. my ground detector doesn’t seems to get correctly the last transform.position.y of my character :S anyways if i jump its seems to do it correctly probably i have something wrong on my codes that’s for sure hehe :smile: