# Adjust postion only in X axis

I have two game objects named A and B, just as follows:

I want to move object B to this virtual line without any rotation so that the destination is B' . What I have are transformation of object A and B. I'm brand new to Unity3D and its APIs, so I don't know how should I get this done.

• You want to translate object B towards the red line or just instantaneously place it on the red line ?? Sep 17, 2015 at 9:19
• I want to project/translate B onto the line. Sep 17, 2015 at 9:25
• @HashBuoy I've edited my question to be more clear. Please take a look. Sep 17, 2015 at 9:36

## 1 Answer

The simplest and non mathematical (noob) way to do this is to make Game object B a child of Game object A , then make local position x as zero and remove B from the parent A.

B.transform.parent = A.transform;
B.transform.localPosition = new Vector3 (0, B.transform.localPosition.y, B.transform.localPosition.z);
B.transform.parent = null;

• Good. I'll add this : If you use an empty gameobject (like C) you copy B.position to C.position. Then you use C instead of B in your code. So finaly you get C.postion as target for a smooth movement of B. Sep 17, 2015 at 9:50
• No probs :).... Sep 17, 2015 at 9:51