x


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.

more ▼

asked May 18 '10 at 06:41 AM

dhendrix gravatar image

dhendrix
2.2k 25 34 59

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?

May 18 '10 at 07:26 AM StephanK

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!

May 18 '10 at 07:35 AM dhendrix

have you tried localPosition?

May 18 '10 at 10:31 AM spinaljack
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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!

more ▼

answered May 18 '10 at 11:10 AM

duck gravatar image

duck ♦♦
41k 92 148 415

Fantastic! This is exactly what I wanted! Thanks!

May 18 '10 at 12:53 PM dhendrix
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1282
x887
x580
x286

asked: May 18 '10 at 06:41 AM

Seen: 2991 times

Last Updated: May 18 '10 at 07:38 AM