# How to convert between local / world coordinates?

I’ve been checking the script reference for: http://unity3d.com/support/documentation/ScriptReference/Transform.TransformPoint.html

But I can’t seem to make it work.

Here is my setup:

I have a large tilebased map (dynamically generated). I have my levelcam and some objects moving along in this map. But sometimes I need to take the object from the view (which has the camera hierachy as parent) and convert its local relative position into a world/level coordiate so I can freeze it in the map instead of moving it around with the cam.

As this happens on user-event, I would like to see how to make the calls if anyone can tell me?

I have the following GameObjects:

• goLevelCam
• goObjectToFreeze (has goLevelCam as parent until frozen)

And in the tilebased map:

• goLevelMap

I thought something like:

``````Vector3 freezePos = goLevelMap.transform.TransformPoint( goObjectToFreeze.transform.position );
goObjectToFreeze.transform.parent = goLevelMap;
goObjectToFreeze.transform.position = freezePos;
``````

But this makes the object jump to 0,0,0 (which is its normal coord in the hierachy as its child-child-child of a bigger setup I have tried to simplify for example usage.

Really hope this makes any sense…

Let me know if you need more explanations.

It seems you are totally confused about all the Transform stuff:

Note that a transformation is not just a position offset. It’s also rotated and scaled according to the parent.

``````Vector3 newPos = goObjectToFreeze.transform.TransformPoint( Vector3.zero );
``````

is the same as

``````Vector3 newPos = goObjectToFreeze.transform.position;
``````

Also in most cases you don’t need to convert much. When you change the parent of a Transform it will keep it’s world position (and rotation / scale). It’s automatically converted. If you want to change the parent of an object and move the object to the parents position do either:

``````childObject.parent = parentObject;
childObject.localPosition = Vector3.zero;
``````

or

``````childObject.parent = parentObject;
childObject.position = parentObject.position;
``````

The first case is the usual way.

Ohhh, I think I’ve got it now.

You need to use the transform of the OBJECT you want to convert into Worldspace, not the destination parent.

So its:

``````Vector3 newPos = goObjectToFreeze.transform.TransformPoint( Vector3.zero );
``````

I just wish to chime in here as I realised something that was not immediately obvious to me when I was working with this.

I had some vector2 Positions that I needed to convert to local positions. (This was due to the fact that in my game the player can rotate the camera and therefore the lerping animation I was doing had to be done within the transform not the world)

My solution was to do this:

``````Vector2 localStartPos = transform.InverseTransformPoint(_StartPosition);
Vector2 localEndPos = transform.InverseTransformPoint(_EndPosition);
``````

May be just my tired brain but thought I’d mention it as it wasnt immediately obvious to me that I could simply declare it that way.