Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thomas-Hawk · Jan 25, 2019 at 03:21 PM · positiondirectionlocalpositiontransformdirectionvector math

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);
 


Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Jan 25, 2019 at 03:44 PM 0
Share

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?

avatar image Thomas-Hawk xxmariofer · Jan 25, 2019 at 03:57 PM 0
Share

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.

avatar image xxmariofer Thomas-Hawk · Jan 25, 2019 at 04:45 PM 0
Share

$$anonymous$$ay i ask why you needed to set the child object using localposition rather than position? did @DCordoba solution worked?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges