World space to local space

I have a line renderer with three points. It has the property “Use world space” set to False, and I need to keep it that way for different reasons.

Sometimes, I need to change the position of the middle point (i.e point [1]). The new position is known in world space. This code is from a script in the line renderer’s object.

private void SetPosition(Vector3 worldPos)
{
Vector3 localPos = worldPos * some matrix magic;
lineRenderer.SetPosition(1, localPos);
}

What should “worldPos * some matrix magic” be changed to?

hah, someone else had the same problem and referred to it as magic!

Just FYI, a quick google would have found you the answer. Doing searches like this can help you get results faster… my advice would be to use these forums when you cant find/describe the problem properly.

Thanks for the reply James.

Are you refering to the posted solution? I may be mistaken, or just don’t get it. But isn’t that the opposite of what I want to do? From what I can see, transform.TransformDirection is for transforming local space to world space. I can’t see how I can use it to go the other way?

And I’m sorry if this seems rather basic. I can surely see your point. But I just can’t get my head around it, and actually our friend google isn’t that clear about the matter either (I asked him before I asked you). But who knows, maybe it’s right in front of me and I just don’t see it.

In my case I took the first search option, didnt notice it was backwards… but there is always an inverse available :wink:

http://docs.unity3d.com/Documentation/ScriptReference/Transform.InverseTransformDirection.html\

Looking at the second link in the search ‘unity world to local space’ took me to the Transform documentation…

Cast your eyes down to the functions section… there are multiple functions there that give you what you want.

Thanks for your patience.

At first I didn’t see the relation between the direction and the position. But by subtracting my “own” tranform.position from the position of the object I wanted to find out the localspace position from, and by using InverseTransformDirection on the difference, I got the right result. Still not completely 100% clear on why, but I guess it makes sense, since the difference would be the relative position (but without caring about rotation), and the InverseTransfromDirection takes care of the different rotations. Or something like that. It’s getting late I think. :slight_smile:

This did the trick:

Vector3 localPos = transform.InverseTransformDirection(theOtherObject.position - transform.position);

Thanks for pushing me in the right direction! Much appreciated.

np.

we are here to help, but are more likely to help if you help yourself first :wink:

Funny. Many many years later, I’m doing this exact same thing and completely forgot how. I toss a half sensible query at Google, and it gives me this response as “here’s a guy asking the exact same question for the exact same usage.” A very good reason to answer questions if you can, and not respond with a google it yourself.
The original poster provided the simple and proper answer himself, making MY query a perfect response.

4 Likes
    //LOCAL TO WORLD
    Vector3 LocalToWorld(Vector3 position, Transform myTransform){
        return myTransform.position + myTransform.rotation*(Vector3.Scale(position, myTransform.localScale));}

    //WORLD TO LOCAL
    Vector3 WorldToLocal(Vector3 position, Transform myTransform){
        Vector3 scaleInvert = myTransform.localScale;
        scaleInvert = new Vector3(1f/scaleInvert.x, 1f/scaleInvert.y, 1f/scaleInvert.z);
        return Vector3.Scale(Quaternion.Inverse(myTransform.rotation)*(position-myTransform.position), scaleInvert);}
2 Likes

Quaternion.Inverse(this.rotation) * (position - this.position) / this.scale;