Created
March 29, 2024 15:42
-
-
Save hariedo/3c655c783ed4b2ca499888549272d5bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.