Align Two Objects at a Child Transform

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.

        // assembly == the gameobject root to move
        // feature == the specific place on the assembly to align with station
        // station == the reference to which the assembly feature will align
        // flip == an additional rotation applied to the assembly pivoting
        //         around the feature (e.g., 180º Y flip)
        //
        public static void AlignAtFeature(
            Transform assembly, Transform feature, Transform station,
            Quaternion flip)
        {
            Assert.IsTrue(assembly != null);
            Assert.IsTrue(feature != null);
            Assert.IsTrue(feature.IsChildOf(assembly));
            Assert.IsTrue(station != null);

            assembly.rotation =
                station.rotation *
                Quaternion.Inverse(
                    Quaternion.Inverse(assembly.rotation) *
                        feature.rotation) *
                flip;

            assembly.position =
                station.position +
                (assembly.position - feature.position);
        }
5 Likes

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

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.

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,

4 Likes

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.

2 Likes

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.

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.

unhappyincomparablechevrotain
dappernauticalblueandgoldmackaw
palatablejoyfularacari

4 Likes

Just implemented this, contrary to some comments in this thread it works great and was just what I was looking for, thanks!

2 Likes