Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by dhendrix · May 18, 2010 at 06:41 AM · transformpositionvector3direction

Finding difference between gameObject's x coordinate according to one object's transform

Okay, forgive the crude illustration, but it's the best way to demonstrate my problem:

alt text

The black boxes are gameObjects, and the red dots are their transform's origin. Likewise the arrows demonstrate their transform's current direction/rotation. I want to find the difference in their x position, according to the bottom left gameObject's transform(red axis). In world coordinates, this would be a number greater than zero, but In my case it should return close to zero, if you get what I'm saying.

Now I thought this would be very simple with transform.right like so:

var diffX = transform.position - other.transform.position;
diffX = Vector3.Scale(diffX ,transform.right);

I also tried:

var tempF = transform.position.x - other.transform.position.x;
var diffX  = Vector3(tempF, 0 ,0);
diffX = transform .TransformDirection(diffX);

Neither one gives the correct result. Any ideas? This one is driving me bonkers.

Comment
Add comment · Show 3
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 StephanK · May 18, 2010 at 07:26 AM 0
Share

I don't get what you want to do here. You said you are looking for the difference in the x-coordinate. Do you mean position? Do you want to know how far the left object has to be moved along it's x-axis to be x-axis-aligned with the other object in world space? Or do you want to change the size of left object to match that of right object?

avatar image dhendrix · May 18, 2010 at 07:35 AM 0
Share

Ah yes, sorry about that. If you look at the two objects x position in world coordinates, you would find that the right gameObject has a higher value since it is further to the right. But if you tilt your head according to the bottom-left gameObject's red axis, you'll see that they have basically the same x position. How can I programmatically find this during runtime? Sorry for not being clear!

avatar image spinaljack · May 18, 2010 at 10:31 AM 0
Share

have you tried localPosition?

1 Reply

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

Answer by duck · May 18, 2010 at 11:10 AM

You need to convert the target object's world position to a relative local position, in relation to the source object. You can do this with InverseTransformPoint.

For instance, attach this to object A (the bottom left object):

var localPosB = transform.InverseTransformPoint(objectB.transform.position);

This means that "localPosB" now contains a vector which represent's object B's position in coordinates relative to object A's orientation and position.

Then, you can get the difference in objectA's local X axis, by simply checking the x component of localPosB, eg:

var diffX = localPosB.x;

Hope this works for you!

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 dhendrix · May 18, 2010 at 12:53 PM 0
Share

Fantastic! This is exactly what I wanted! Thanks!

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

No one has followed this question yet.

Related Questions

Why are my player's positions not being set correctly? 1 Answer

How to rotate an object to face the direction it's going? 1 Answer

How do I freeze a gameObject when MouseDrag runs over a certain Vector3? 1 Answer

Setting target position in Vector3.MoveTowards 2 Answers

Get LookAt vector of a tranform 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