Is there a way to make an object follow the movement and rotation of a line between 2 spheres?

Hi, I’m currently working on a project where the program takes inputs from an outside script and map it to spheres. But, now, for an example, let’s say we have only 2 spheres, and I want to map an object to said spheres where if the 2 spheres moved a line between them will form, and my object will be parallel to that line.

Pics of what I want:-

Can this be done? and if yes then how?

To create the line, you can use LineRenderer.

To align the tube object with the line, just take the vector from sphere A to sphere B and align the object to it. Something like this:

Vector3 line = sphere1.transform.position - sphere2.transform.position;
tube.transform.rotation = Quaternion.Euler(line);

Hi, and thanks for the fast replay, but I have already tried that and the results were something like this 9173666--1277252--upload_2023-7-26_20-42-13.png

I have been stuck with this problem for more than I’d like to admit, and I have no idea how to approach it anymore.

Looks like it’s aligned but just the local coordinates are 90 degrees off in the direction that is right in your picture. Find out which local axis that is by selecting the tube. Then add a 90 degree rotation around that axis.

So, if the local axis to the right in your picture is the X axis, do this:

tube.transform.rotation = Quaternion.Euler(line) * Quaternion.Euler(90f, 0f, 0f);

This is complete nonsense:

Quaternion.Euler works with rotation angles around each axis. Relative position vector is not a set of angles.

For something like this you will want to use Quaternion.FromToRotation or Quaternion.LookRotation .

Vector3  line = sphere1.transform.position - sphere2.transform.position;
tube.transform.rotation =  Quaternion.FromToRotation(Vector3.up, line.normalized);

The first argument of FromToRotation will depend on the default orientation of tube object. Might have to use Vector3.forward instead of up if the tube is normally horizontal.

True, didn’t test my code and was tired. I stand corrected. Your code looks correct.

        cylinder.transform.position=sphere1.transform.position+(sphere2.transform.position-sphere1.transform.position)/2; // set to midway position
        cylinder.transform.up=(sphere2.transform.position-sphere1.transform.position).normalized; // direction
        cylinder.transform.position+=cylinder.transform.right; // make parallel