Skip to main content

ArmController

A class in Root.Township, Alta.Character.

Hand-written arm solver for a character dummy: each update it rotates the clavicle, shoulder, elbow, forearm and wrist bones to reach the ghost hand, with slight stretch. One per arm.

Constructors​

MemberWhat it is
ArmController()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

33 private and internal fields
MemberWhat it is
private BoneIdentifier chestBone ID of the chest.
private Vector3 chestToAbdomenChest axis pointing down to the abdomen, for this update.
private Vector3 chestToArmChest axis pointing out towards this arm, for this update.
private Vector3 chestToFrontChest axis pointing forward, for this update.
private BoneIdentifier clavicleBone ID of the clavicle.
private float clavicleLengthMeasured clavicle length.
private Vector3 clavicleToFrontClavicle forward direction after rotation.
private CharacterDummyPointer dummyPointer to the character dummy whose rig is posed.
private BoneIdentifier elbowBone ID of the elbow.
private Vector3 elbowOriginalPositionElbow rest local position, restored each update before stretching.
private BoneIdentifier forearmBone ID of the forearm.
private float forearmLengthMeasured forearm length.
private Vector3 forearmOriginalPositionForearm rest local position, restored each update before stretching.
private HandTargetHandler ghostHandHandler that provides the hand target transform.
private Quaternion handRotationGhost hand rotation for this update.
private BoneIdentifier headBone ID of the head.
private float lastTwistAngleTwist amount from the previous update, used to avoid flips.
private static Logger loggerNLog logger for this class.
private static Quaternion lookRotationComplementFixed rotation that maps the rig's bone axes onto Unity's look rotation.
private float maxStretchLargest extra length the arm may stretch to reach the hand.
private Vector3 oldElbowPullSmoothed elbow pull point from the previous update.
private ArmController otherArmBacking field for OtherArm.
private BoneIdentifier shoulderBone ID of the shoulder.
private Vector3 shoulderPositionShoulder world position for this update.
private HandIndex sideBacking field for Side.
private float sideMultiplier-1 for the left arm, 1 for the right, used to mirror axes.
private float upperArmLengthMeasured upper arm length.
private float upperToLowerArmStretchRatioHow stretch is split between the upper arm (elbow) and forearm.
private BoneIdentifier wristBone ID of the wrist.
private Vector3 wristBackBack-of-hand direction for this update.
private Vector3 wristPositionGhost hand position for this update.
private Vector3 wristThumbThumb direction for this update.
private Vector3 wristToArmDirection from the wrist back along the arm for this update.

Properties​

MemberWhat it is
Transform Chest { get; private set; }The chest bone.
Transform Clavicle { get; private set; }The clavicle bone.
Transform Elbow { get; private set; }The elbow bone (upper arm end).
Transform Forearm { get; private set; }The forearm twist bone.
Transform GhostHand { get; }The hand target transform the arm reaches for.
Transform Head { get; private set; }The head bone.
ArmController OtherArm { get; }The arm controller for the other side.
Transform Shoulder { get; private set; }The shoulder (upper arm) bone.
HandIndex Side { get; }Which hand this arm is.
Transform Wrist { get; private set; }The wrist (hand) bone.

Methods​

MemberWhat it is
override void UpdateCharacterComponent()Runs the full arm solve for this frame.
14 private and internal methods
MemberWhat it is
private void Awake()Looks up the arm bones in the dummy's rig map and remembers the elbow and forearm rest positions.
private Vector3 CalculateElbowPull(out float shoulderToWritDistance)Works out a smoothed point the elbow is pulled towards, based on wrist and chest positions.
private Vector3 CalculateIdealElbowUp(Vector3 upperArmNormalized, Vector3 forearmNormalized)Works out the preferred up direction for the elbow from the upper arm and forearm directions.
private void GetArmVectors(out Vector3 upperArmNormalized, out Vector3 forearmNormalized)Solves the elbow position and returns the normalised upper arm and forearm directions.
private void GetInitialVectors()Reads the ghost hand pose and chest axes for this update and resets elbow and forearm positions.
private void GetShoulderVectors(Vector3 clavicleUp)Updates the shoulder position and the clavicle forward direction after the clavicle is rotated.
private void GetUpVectors(out Vector3 shoulderUp, out Vector3 elbowUp, out Vector3 forearmUp, Vector3 upperArmNormalized, Vector3 forearmNormalized)Gets the up vectors for shoulder, elbow and forearm, including shoulder limit and twist.
private void HandleArmTwist(ref Vector3 elbowUp, Vector3 upperArmNormalized, Vector3 forearmNormalized, out Vector3 wristThumbForwardRemoved)Rotates the elbow up vector to follow the wrist's twist, avoiding sudden flips.
private Vector3 IKElbowPull(Vector3 elbowPull, float shoulderToWrist)Two-bone IK step: returns the upper arm direction that bends towards the elbow pull point.
private Vector3 LimitShoulder(Vector3 idealElbowUp)Pushes the elbow up vector towards the clavicle's front so the shoulder does not twist too far.
private Quaternion LookRotation(Vector3 forward, Vector3 up)Look rotation corrected for this rig's bone axes and the arm's side.
private void RotateArm()Rotates shoulder, elbow, forearm and wrist and shifts the elbow and forearm to stretch towards the hand.
private Quaternion RotateClavicle()Rotates the clavicle towards the hand and returns its new rotation.
private void Start()Measures clavicle, upper arm and forearm lengths and sets the side multiplier.