- Home /
Local Point to World Point without TransformPoint()
Greetings,
I really would like to know, how to convert a local vector3 position to a world vector3 position. And I wanna do this without having the corresponding position on a transform. Tryed searching, but all I find is TransformPoint() and I know pretty much nothing when it comes to math. I only have two Vector3s, and creating a gameobject just to use that method seems pretty stupid. pls help me out! :)
Well, like any coordinate conversion, it helps to know the reference frames you're dealing with.
Where the this point co$$anonymous$$g from? What is it relevant to?
What are you talking about? What am I missing?
There are 3 positions here. The World wich obviously is (0,0,0) a Point A that is in the world, lets say at (5,8,2) and theres a point B in Point A, lets say that one is at (4,2,9)
Now I wanna know where Point B is in the World.
If your heirarchy is something like this:
Point A (5,8,2) (with child Point B (4,2,9))
Then add Point A's reference frame to Point B. Point B would be:
(4+5,8+2,2+9) --> (9,10,11)
Answer by villevli · Nov 21, 2016 at 03:00 PM
You can achieve what you want using the Matrix4x4 class.
Give a position(Vector3), rotation(Quaternion) and scale(Vector3), just like a Transform has:
Matrix4x4 m = Matrix4x4.TRS(position, rotation, scale);
This works like Transform.TransformPoint(localPos)
Vector3 worldPos = m.MultiplyPoint3x4(localPos);
This works like Transform.InverseTransformPoint(worldPos)
Vector3 localPos = m.inverse.MultiplyPoint3x4(worldPos);
You're probably going to have rotation as Quaternion.identity
and scale as Vector3.one
if you only want to deal with positions.
In that case it's actually simpler to just say :
Vector3 worldPos = localPos + position;
Vector3 localPos = worldPos - position;
position
is the same Vector3 you would have given to the Matrix4x4.TRS method. It's that simple if there's no rotation or scaling.
This was really helpful, thank you for putting it together.
Answer by ToasterKyle · Nov 20, 2016 at 01:56 PM
Why do you have to convert the position from local to world space? Are the Point A and Point B parented to something? If not, simply use transform.position(world space)
Your answer
Follow this Question
Related Questions
Class member Vector3 based on local transform 1 Answer
localposition not working? Calculating a vector in front of an object. Picture and code included 1 Answer
Transfer axis global rotation on local axis of another object 0 Answers
Adding a local position with InverseTransformPoint works in z axis, but inverts in x axis 0 Answers
Locking gameobject to rotating floor 0 Answers