Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Med_o, Jul 26, 2023.

  1. Med_o

    Med_o

    Joined:
    Jul 9, 2023
    Posts:
    2
    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:-
    upload_2023-7-26_19-6-40.png

    upload_2023-7-26_19-3-58.png

    Can this be done? and if yes then how?
     

    Attached Files:

  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    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:


    Code (CSharp):
    1. Vector3 line = sphere1.transform.position - sphere2.transform.position;
    2. tube.transform.rotation = Quaternion.Euler(line);
     
  3. Med_o

    Med_o

    Joined:
    Jul 9, 2023
    Posts:
    2
    Hi, and thanks for the fast replay, but I have already tried that and the results were something like this 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.
     
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    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:

    Code (CSharp):
    1. tube.transform.rotation = Quaternion.Euler(line) * Quaternion.Euler(90f, 0f, 0f);
     
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    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 .

    Code (CSharp):
    1. Vector3  line = sphere1.transform.position - sphere2.transform.position;
    2. 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.
     
    arkano22, Bunny83 and KillDashNine like this.
  6. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    True, didn't test my code and was tired. I stand corrected. Your code looks correct.
     
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.         cylinder.transform.position=sphere1.transform.position+(sphere2.transform.position-sphere1.transform.position)/2; // set to midway position
    2.         cylinder.transform.up=(sphere2.transform.position-sphere1.transform.position).normalized; // direction
    3.         cylinder.transform.position+=cylinder.transform.right; // make parallel