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 /
avatar image
0
Question by useless.unity.user · May 21, 2012 at 02:59 AM · transformposition

Get a gameobject position relative to another?

How would I get the position of one gameobject relative to another (where the axis align)? Thanks!

Comment
Add comment
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

1 Reply

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

Answer by aldonaletto · May 21, 2012 at 03:41 AM

Question too vague! Anyway, you can use something like this:

var obj1: GameObject; // drag the objects to these fields in the Inspector var obj2: GameObject;

function Start(){ var relativePos = obj1.transform.position - obj2.transform.position; print("relative position: "+relativePos.ToString()); } NOTE: When an object is childed to another, its transform.localPosition holds exactly the position relative to its parent in the parent's local space: if the child is 1 unit ahead of the parent, for instance, its transform.localPosition will be (0,0,1), no matter which's the parent rotation. In some cases it's better to child the object to some parent and use its localPosition.

Comment
Add comment · Show 4 · 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 useless.unity.user · May 21, 2012 at 04:06 AM 0
Share

my apologies, for example I have two objects and I want the Vector3 of one object relative to the other so that their axis align. I guess its like parenting, but I don't want to actually have to parent first. Hope this clarifies it a bit.

avatar image aldonaletto · May 21, 2012 at 04:42 AM 0
Share

Not clear enough...
If you want to keep obj2 in the same Z coordinate as obj1, for instance, you can just copy the desired coordinate:

 function Update(){
   obj2.transform.position.z = obj1.transform.position.z;
 }

This will keep both objects at the same plane perpendicular to Z.

But if you want to align the axes of two objects, you can copy transform.rotation, like this:

 function Update(){
   obj2.transform.rotation = obj1.transform.rotation;
 }
avatar image useless.unity.user · May 21, 2012 at 06:24 AM 0
Share

sorry I'm not so good at explaining things. But I think .InverseTransformPoint is what I wanted to use. Thank you for working with me on this!

avatar image aldonaletto · May 21, 2012 at 11:31 AM 0
Share

Ok, it's the relative position in local space - the same as localPosition, but without the parent link, as you've tried to explain.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.position 3 Answers

Difference between transform.forward and transform.position.z? 1 Answer

Storing transforms from objects in Array 1 Answer

Place a object in front the camera in the coordinate 0 of Y axis 2 Answers

Change object position on trigger enter 3 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