1
\$\begingroup\$

I have a GameObject (goA) with a LineRenderer that has a single line drawn, on which I'd like to place cylinders so the top of the cylinder points to each end of the straight line that the cylinder sits on, (similar to a cylindrical bead on a straight piece of string, or a Candlestick chart).

The cylinder GameObject (goB) is a child of the goA which the LineRenderer is sat on (goB.transform.parent == goA.transform).

goA is not rotated itself, and the Line drawn could be any angle relative to the GameObject, and it would be a bit of a hassle to refactor so that the Line is always vertical and instead rotate goA, and it seems this should be an easily solvable problem!

I'm attempting to use either LookRotation or LookAt on the cylinder but for whatever reason all permutations I can think of simply are not working.

Thanks for any advice!

\$\endgroup\$
2
  • \$\begingroup\$ I'm just going to ask out of curiosity - why are you needing the LineRenderer to create the cylinder; what is the inspiration behind doing it in this particular way? \$\endgroup\$ Commented Feb 24, 2020 at 9:51
  • \$\begingroup\$ @Tom'Blue'Piddock sorry maybe I'm unclear, the LineRenderer draws a line on one GameObject (A), and the cylinder is on a second GameObject (B). They are only connected by B.transform.parent == A.transform \$\endgroup\$
    – DanH
    Commented Feb 27, 2020 at 10:28

1 Answer 1

1
\$\begingroup\$

First, get the direction from the start of the line to the end:

var direction = endTransform.position - startTransform.position;

Then form an orientation that rotates the up vector to that direction:

var orientation = Quaternion.FromToRotation(Vector3.up, direction);

cylinderTransform.rotation = orientation;
\$\endgroup\$
0

You must log in to answer this question.

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