- Home /
Getting a childs global position.
Hi, I have a model which consists of several meshes eg, walls, doors etc. They are all grouped under one gameobject eg house. I set this gameobject at position(0,0,0). Now the wall at the back of the house is obviously not at position (0,0,0). However I'm not sure how exactly to find this position. I've tried transform.position and transform.localPosition but these just print out the position(0,0,0), which is not what I want. Can anyone please help me?
Answer by Lo0NuhtiK · Jan 25, 2012 at 02:46 AM
Should work just fine if you're putting it on the child object(s).
transform.position does give the world coordinates.
If it's not child objects you're talking about, and just other parts of one big object, then you could make empty child objects and position them where you want them to be and then use those coord's.
Put this on a child object and hit "P" to see the debug's in the console. ->
var myTrans : Transform ;
var myParent : Transform ;
function Start(){
myTrans = transform ;
myParent = transform.root ;
}
function Update(){
if(Input.GetKeyDown(KeyCode.P)){
Debug.Log("My world position = " + myTrans.position) ;
Debug.Log("My position relative to my parent = " + myTrans.localPosition) ;
Debug.Log("My parent's world position = " + myParent.position) ;
}
}
$$anonymous$$aybe my ter$$anonymous$$ology is incorrect. I think you're right when you say it's not a child object and that it is just other parts of one big object.
Since this one big gameobject is huge, it's not practical for me to place empty child objects at every position. Is there any other way I could get the position of a component of the gameobject?
I guess lots of math lol ...
Idk... I use empty game objects for all sorts of stuff. I don't think they cost anything on performance at all, they're... empty ...
You could use vertex positions to find your area's also I suppose.
Can you make a screenshot of your object in your hierarchy so we can make sure it doesn't have children on it and something else isn't happening/wrong/whatever before you start going through all the extra effort?
I'm trying to get the object position fro a raycast and hit so would that be causing the problem? $$anonymous$$aybe there is something extra I have to do in this case.
Only thing I can think would cause that to return only the parent position is if
1) It's all one big object with no children like we wonder already.
-OR-
2) The child object(s) don't have any colliders on them and you're ray is being cast through them and hitting the parent collider.
Idk. Be easier to try and figure out on this side with a hierarchy screenshot to see if it's one object or one with children since you're not sure what it is.
Ok here's the screen shot as requested:
http://imageshack.us/photo/my-images/444/screenshot20120127at322.png/
In this case "rac_basic_sample..." is the model and the "Basic Wall..." are the children of the gameobject (I assume). I would like to get the position of "Basic Wall...." rather than the position of "rac_basic_sample..." (which I have set to 0,0,0).
Also would you happen to know if Unity allows for a wildcard search so that I can somehow find the gameobject with the [117685] identifier? I'm trying to highlight specific objects without having to use their full names.
Also thank you for your help so far.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063350im_/https://answers.unity.com/themes/thub/images/avi.jpg)