Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Kaze_Senshi · Mar 17, 2012 at 03:01 AM · 2drotationtransformpositionmoving platform

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
22
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kaze_Senshi · Mar 17, 2012 at 03:14 PM 0
Share

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

avatar image andyrev · Apr 22, 2020 at 11:28 AM 2
Share

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

avatar image KrisTUD andyrev · Jun 26, 2020 at 03:36 PM -1
Share

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

avatar image Bunny83 KrisTUD · Jun 26, 2020 at 04:05 PM 2
Share

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

avatar image AppleseedSmoothie andyrev · Apr 18, 2021 at 02:32 AM 0
Share

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

avatar image triangle4studios andyrev · Nov 25, 2021 at 05:47 AM 0
Share

For nested objects, transform.position ABSOLUTELY gets the parent position.

avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_PNVpFX7xnCpieQ · Nov 23, 2020 at 07:13 AM -1
Share

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

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image puppeteer · Apr 14, 2017 at 02:06 PM 1
Share

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.

avatar image
0

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to calculate the real position after rotate with a original point without using Transform in 2D game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges