Skip to content

Instantly share code, notes, and snippets.

@neon-izm
Last active May 19, 2025 13:19
Show Gist options
  • Save neon-izm/3aa3c0fe30e4ddbceb423f1b3839be5a to your computer and use it in GitHub Desktop.
Save neon-izm/3aa3c0fe30e4ddbceb423f1b3839be5a to your computer and use it in GitHub Desktop.
Use for FinalIK
using System.Collections;
using System.Collections.Generic;
using RootMotion;
using UnityEngine;
public class HandIkAxisSettingSolver : MonoBehaviour
{
[SerializeField] private Transform foreArm;
[SerializeField] private Transform hand;
// Start is called before the first frame update
void Awake()
{
if (foreArm != null && hand != null)
{
var wristToPalmAxis = GuessWristToPalmAxis(hand, foreArm);
Debug.Log("WristToPalmAxis");
Debug.Log (wristToPalmAxis);
}
{
var palmToThumbAxis = GuessPalmToThumbAxis(hand, foreArm);
Debug.Log("PalmToThumbAxis");
Debug.Log (palmToThumbAxis);
}
}
public static Vector3 GuessWristToPalmAxis(Transform hand, Transform forearm)
{
Vector3 toForearm = forearm.position - hand.position;
Vector3 axis = AxisTools.ToVector3(AxisTools.GetAxisToDirection(hand, toForearm));
if (Vector3.Dot(toForearm, hand.rotation * axis) > 0f) axis = -axis;
return axis;
}
public static Vector3 GuessPalmToThumbAxis(Transform hand, Transform forearm)
{
if (hand.childCount == 0)
{
Debug.LogWarning("Hand " + hand.name + " does not have any fingers, VRIK can not guess the hand bone's orientation. Please assign 'Wrist To Palm Axis' and 'Palm To Thumb Axis' manually for both arms in VRIK settings.", hand);
return Vector3.zero;
}
float closestSqrMag = Mathf.Infinity;
int thumbIndex = 0;
for (int i = 0; i < hand.childCount; i++)
{
float sqrMag = Vector3.SqrMagnitude(hand.GetChild(i).position - hand.position);
if (sqrMag < closestSqrMag)
{
closestSqrMag = sqrMag;
thumbIndex = i;
}
}
Vector3 handNormal = Vector3.Cross(hand.position - forearm.position, hand.GetChild(thumbIndex).position - hand.position);
Vector3 toThumb = Vector3.Cross(handNormal, hand.position - forearm.position);
Vector3 axis = AxisTools.ToVector3(AxisTools.GetAxisToDirection(hand, toThumb));
if (Vector3.Dot(toThumb, hand.rotation * axis) < 0f) axis = -axis;
return axis;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment