- Home /
Matrix to transform bone orientation to world orientation
I have a need to manipulate bones in my scripts, as well as get transform information from them. When my bones import, they have unusual directions relative to the parent mesh.
For instance, if my object faces z forward, its bones are facing z up!
I have gotten around this by creating a temporary gameobject, parenting the bone to the gameobject, and manipulating that; like so:
private void TransformBonesAlongCurve(LinkedList<Transform> bones, BezierCurve curve, float percentage = 1, Color debugColor = default(Color)) {
// TODO: can this just be a static object we keep somehere so we can avoid creation/destruction?
GameObject guide = new GameObject("Guide");
float lineSteps = bones.Count;
int count = 1;
Vector3 lastPoint = curve.GetPoint(0);
foreach (Transform bone in bones) {
float t = MathExtensions.Normalize01(1, lineSteps, count) * percentage;
guide.transform.position = bone.position;
Transform parentBuffer = bone.parent;
bone.parent = guide.transform;
guide.transform.position = curve.GetPoint(t);
guide.transform.rotation = Quaternion.LookRotation(transform.InverseTransformDirection(curve.GetDirection(t)));
bone.parent = parentBuffer;
//Debug.DrawLine(lastPoint, curve.GetPoint(t), debugColor, 10000);
lastPoint = curve.GetPoint(t);
count++;
}
Destroy(guide);
}
However, this feels clunky and hacky. It works, but I would prefer to do this the "right" way with a matrix operation. How would one re-write the function above to use a Matrix4x4 instead of the temporary gameobject?
Your answer
![](https://koobas.hobune.stream/wayback/20220612050920im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Manually set transform on certain bones in skinnedmeshrenderer 1 Answer
Is there such a thing as Unity Algebra? 0 Answers
"Baking" Object scale into mesh 1 Answer
Calculation behind camera.ScreenToWorldPoint 0 Answers
Why is transform.up different than transform.localToWorldMatrix.MultiplyVector(Vector3.up) ? 0 Answers