Skip to content

Instantly share code, notes, and snippets.

@hariedo
Created March 29, 2024 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hariedo/3c655c783ed4b2ca499888549272d5bf to your computer and use it in GitHub Desktop.
Save hariedo/3c655c783ed4b2ca499888549272d5bf to your computer and use it in GitHub Desktop.
// 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);
}
@hariedo
Copy link
Author

hariedo commented Mar 29, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment