1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\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, 2015 at 9:19
  • \$\begingroup\$ I want to project/translate B onto the line. \$\endgroup\$
    – frogatto
    Sep 17, 2015 at 9:25
  • \$\begingroup\$ @HashBuoy I've edited my question to be more clear. Please take a look. \$\endgroup\$
    – frogatto
    Sep 17, 2015 at 9:36

1 Answer 1

3
\$\begingroup\$

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;
\$\endgroup\$
2
  • 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\$ Sep 17, 2015 at 9:50
  • \$\begingroup\$ No probs :).... \$\endgroup\$
    – Hash Buoy
    Sep 17, 2015 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .