Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by DDP · Nov 11, 2012 at 10:08 PM · relative position

Relative position

Hi,

let's say I have 2 objects: ObjectA and ObjectB. I want to know what's the position of ObjectB relative to ObjectA. I want the returned position to use the axes of ObjectA (to consider its rotation). I do NOT want ObjectB to be parent of ObjectA. They need to be separated objects.

In other words, the effect I am looking for is the same than parenting the two objects and using ObjectA.transform.localPosition, but I cannot parent these objects.

Is there any magic inside Unity I didn't find yet? Thanks a lot for your time, it's most appreciated!

Comment
Add comment · Show 1
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 mthicke2 · Apr 26, 2017 at 09:16 PM 0
Share

Is it possible to do this with only a Vector3 for the origin position? (No Transform)?

3 Replies

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

Answer by Graham-Dunnett · Nov 11, 2012 at 10:22 PM

Isn't this a maths question? AB = posB - posA will give you the vector from A to B. Then get right, up, and forward from the ObjectA's transform. Make sure that these are normalised, then take the dot product of AB with each of these normalised vectors, to get 3 values which are the components of the position of B relative to the axes of A.

Comment
Add comment · Show 1 · 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
avatar image DDP · Nov 11, 2012 at 10:56 PM 0
Share

Thanks a lot! I can't find how to accept your answer :(

EDIT: Ok, I found the check mark!

avatar image
8
Wiki

Answer by DDP · Nov 12, 2012 at 02:48 AM

Thanks a lot! I'll just post the code I ended up with.

 public static Vector3 getRelativePosition(Transform origin, Vector3 position) {
     Vector3 distance = position - origin.position;
     Vector3 relativePosition = Vector3.zero;
     relativePosition.x = Vector3.Dot(distance, origin.right.normalized);
     relativePosition.y = Vector3.Dot(distance, origin.up.normalized);
     relativePosition.z = Vector3.Dot(distance, origin.forward.normalized);
     
     return relativePosition;
 }
Comment
Add comment · Show 1 · 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
avatar image
3

Answer by fipsy · Oct 31, 2018 at 01:34 PM

Actually Unity has a built in function for that: Transform.InverseTransformPoint. If you want to get the position of objectB relative to objectA, your code should look like this:

 Vector3 relativePosition = objectA.transform.InverseTransformPoint(objectB.transform.position);


Comment
Add comment · Show 2 · 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
avatar image ma2doctor · Jul 30, 2020 at 08:39 PM 0
Share

actually not, this will get you totally different results than the solution above, which returns exactly the localPosition equivalent, but in the World's coordinates...

avatar image fipsy ma2doctor · Jul 30, 2020 at 09:50 PM 0
Share

Hey, you were totally right. I don't know what I did in my original answer :S but now my example returns the correct position. Thanks for your reply

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

14 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

Related Questions

How to move an object and keep its distance relative to the player? 2 Answers

Setting children position to Vector3.zero doesn't make them go to center of parent? 1 Answer

Instantiate Object at random position relative to another object 2 Answers

Instantly rotating and changing an object's velocity relative to another (probably quaternion mistake!) 1 Answer

Instantiating an Object Relative to its Parent 2 Answers


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