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:
// which is the same as:
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
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?
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 ristophonics
Apr 12 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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
6 People are following this question.
why is my object's rotation affecting it's position?
How to make Camera position Independent of its Rotation?
Simultaneously translating and rotating 2D sprite?
setting transform physics2d issue
Position and rotate 3 sprites perpendicular to the edges of a triangle