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

enter image description here

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.

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

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;
| improve this answer | |
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – dnk drone.vs.drones Sep 17 '15 at 9:50
  • \$\begingroup\$ No probs :).... \$\endgroup\$ – Hash Buoy Sep 17 '15 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.