- Home /
How to get a vector as if it were a local position to an object?
I have a an object. A script on the object takes a vector, and moves the objects local position to that vector.
So if the vector is 0,0,1, obviously the local position will be "1 unit in front of the parent".
However, adding 0,0,1 to the position obviously just moves it "1 unit forward in world space from the parent."
How do I take this vector - 0,0,1 - and turn it into "0,0,1 but as if it were a local position to the parent?" i
Example to illustrate the problem further: If I say "Set local position to 0,0,1" then the object will move to 1 unit in front of its parent. BUT! I don't have an object. I don't have an actual transform, I just need to calculate one. So I need to be able to set the same position in the same rotation-respecting way. If that is confusing please tell me. Maybe what I am doing is impossible. I do not understand transformdirection and transformpoint. They aren't working the way the documentation presents them.
Edit :I believe a problem may be a simple order of operations issue combined with a possible misunderstanding of transform.inversetransformdirection.
I think that is my solution, but also probably going to try and use empty gameobjects to more easily work with the information.
My application of the accepted answer, I really hope this helps someone:
//Define a vector, "the intended local position converted into its world space relative to the object A"
Vector3 tp = (obj.transform.TransformPoint(obj.steps[i].pos));
//Define a vector, make the handle, passing that last vector.
Vector3 handle = Handles.PositionHandle((tp), Quaternion.identity);
//Set the original intended local position vector to the inverse of where the handle is!
obj.steps[i].pos = obj.transform.InverseTransformPoint(handle);
well i dont understand your exact issue, what are you trying to accomplish? to make a secon object have the same rotation and position than the first (child) gameobject? or what do you mean that you need to calculate one transform? if you want the second object to have the same world position just use the transform.position, is there something i am missing?
Thank you for commenting, let me try to clarify more.
2 Objects, where 1 is a parent (A) to the other, a child (B)
The parent A has a script that stores a vector3, and says "$$anonymous$$ove the child B object to localPosition being that vector 3."
But, what I need to calculate, is suppose there is another, unrelated object / transform "C". If I wanted the child B to move there, I could of course say "$$anonymous$$ove the position of B to that position C". But, if I absolutely need to say "localPosition", so I needed to get that world position vector of C converted to a point as if it were a local position to the parent A, how would I get that?
The ultimate reason is this https://answers.unity.com/questions/1594326/using-positionhandle-to-set-a-a-directional-vector.html but I am trying to simplify the base problem of the question to net more answers.
Imagine I subtract the two positions, and use the result to set the local position. Then I get accurate results in terms of, "0,0,0" becomes the center of the object, but only if the rotation is 0. Because if the difference, for example, is 0,0,1, and the parent A rotates in the world, that 0,0,1 will not move. That's the problem. that 0,0,1 needs to change when the parent rotates, so that the local position when set to 0,0,1 will be the same point in the world.
$$anonymous$$ay i ask why you needed to set the child object using localposition rather than position? did @DCordoba solution worked?
Answer by Deathdefy · Jan 25, 2019 at 04:53 PM
I think what you are referring to is this.
ObjectOne.transform.InverseTransformPoint(ObjectC.transform.position)
This will transform said position of objectc into local space of that object one.
https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html
Your answer
Follow this Question
Related Questions
How to rotate an object to face the direction it's going? 1 Answer
Get instantiated prefab position from Collision Trigger 0 Answers
Camera rotation around player while following. 6 Answers
Determining if something is on the Left or Right of an Object 1 Answer
Calculate a new position instead of using Vector3.back 1 Answer