Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Showcase Align Two Objects at a Child Transform

Discussion in 'Scripting' started by halley, Jan 16, 2023.

  1. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,363
    Every now and then I want to align some object to another object, but relative to a specific part. Spaceship to dock, at the spaceship's airlock door. Road tile to road tile, at the painted stripe. Inventory item to hand bone, at the handle. And so on. It's a very regular recurring problem, and thinking about Quaternions and inverse transforms always takes a minute.

    I could throw this on GitHub as a gist, but just in case anyone else wants to find it via google, here ya go.

    Code (CSharp):
    1.         // assembly == the gameobject root to move
    2.         // feature == the specific place on the assembly to align with station
    3.         // station == the reference to which the assembly feature will align
    4.         // flip == an additional rotation applied to the assembly pivoting
    5.         //         around the feature (e.g., 180º Y flip)
    6.         //
    7.         public static void AlignAtFeature(
    8.             Transform assembly, Transform feature, Transform station,
    9.             Quaternion flip)
    10.         {
    11.             Assert.IsTrue(assembly != null);
    12.             Assert.IsTrue(feature != null);
    13.             Assert.IsTrue(feature.IsChildOf(assembly));
    14.             Assert.IsTrue(station != null);
    15.  
    16.             assembly.rotation =
    17.                 station.rotation *
    18.                 Quaternion.Inverse(
    19.                     Quaternion.Inverse(assembly.rotation) *
    20.                         feature.rotation) *
    21.                 flip;
    22.  
    23.             assembly.position =
    24.                 station.position +
    25.                 (assembly.position - feature.position);
    26.         }
    27.  
     
    spiney199, Bunny83 and Kurt-Dekker like this.
  2. CPU_USAGE

    CPU_USAGE

    Joined:
    Jan 15, 2023
    Posts:
    20
    You’ll never do it with quaternion.

    you are either facing a direction or you are facing the opposite direction. 1 or -1, what direction do you wish to compare to.

    Quaternion does not need to exist for a rotation system to function.

    Imagine each object has a circle around it. 360 to C number of decimal place units of space around that circle perim, this correspond to view port X width will select its range from these circle perimeter. If two objects have a rotation values x y and z as different on all axis. How do I determine the forward vector from y value with no prior context?

    Magnitude XZ
    Magnitude YZ
    Magnitude XY

    obtain angle of comparison or distance axis
     
  3. CPU_USAGE

    CPU_USAGE

    Joined:
    Jan 15, 2023
    Posts:
    20
    In context my friend


    And I mean this in no way to cause offense


    You have coded since 2013. And you have problem with quaternion still. Go to the drawing board and use eulers.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,912
    Are you by any chance related to ANIMALMAN? Because your posts seem to hold his signature :). What halley posted was not a question but a showcase of a common usecase. Everything rotation related in Unity is done with quaternions and that is for a good reason. So your very first statement is just so out of place.
    Ok, please show your code how you can arrange the parent of a complex object in such a way that one of its childs is oriented a certain way, like the orientation of another object just by using euler angles. That would be a fun exercise.

    Just in case you haven't understood the actual problem that we want to solve, here's an older similar question on UA,
     
    Last edited: Jan 17, 2023
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Yeah it's pretty clearly AnimalMan 3.0 (4.0??). Hopefully the mods see right through him.

    Least they're still good for a laugh.

    And I've only been coding two years, and yes I have problems with Quaternions, but that doesn't mean I would use Euler angles over Quaternions because there's a very good reason they exist.
     
    Last edited: Jan 17, 2023
    halley and Bunny83 like this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Your code could be extremely useful to some people doing procgen.

    I know this is a much heavier "ask" than just putting it in a gist, but may I suggest you craft a tiny little project around this snippet to show it off?

    Perhaps you could have a few different "room" prefabs (just made using Unity Cubes) and put doorway markers on them for attaching, then have a loop that picks one, uses your code to graft it onto the previous, yields half a second, and then continues.

    I have my github MakeGeo project and there is an area where I have included other folks code with attributions. If you like I could perhaps whip something up in there and put your code in, with your permission of course.
     
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,363
    Kurt, you're welcome to add it to MakeGeo with attribution.

    With sufficient UnityEditor extras (or runtime equivalents), this function is the key to easy attachables.