Hello,
i’ll try to get a unity script in ue4 bp or c++. It is for an aiming animation via ue4 inside ik so for a Two Bone IK node. The sense of the script is to move the weapon sight which is a scene component to a certain point so basically the camera. Cause at two bone ik i can only set the bone as the ik target and not just the sight component of my weapon.
So i just need to move the hand relativ to the sight scene component which is attached to the weapon.
Unity script:
// Gets the position of the hand relative to the position, rotation and scale of the weapon. That is basically like getting the localPosition of the hand if it was parented to the weapon
Vector3 handPosRelToWeapon = weapon.transform.InverseTransformPoint(ik.solver.bone3.transform.position);
// Just the same logic as above, but for the rotation. You can think of handRotRelToWeapon as the localRotation of the hand bone if it was parented directly to the weapon. If Unity had such a method, it would be called Transform.InverseTransformRotation(Quaternion rotation) just like InverseTransformPoint above and we could write: Quaternion handRotRelToWeapon = weapon.transform.InverseTransformRotation(ik.solver.bone3.transform.rotation); instead of the line below.
Quaternion handRotRelToWeapon = Quaternion.Inverse(weapon.transform.rotation) * ik.solver.bone3.transform.rotation;
// Quaternion.Inverse(rotation1) * rotation2 operation returns rotation2 relative to rotation1 just like Quaternion.Inverse(transform.parent.rotation) * transform.rotation = transform.localRotation;
// Place the IK target relative to weaponAimTarget. We are basically getting the hand position from weapon space and converting it to weaponAimTarget space, so if the weapon is moved with IK to match weaponAimTarget, the other hand will remain locked to the weapon the same way.
ik.solver.IKPosition = weaponAimTarget.TransformPoint(handPosRelToWeapon);
// Hand rotation is converted from weapon space to weaponAimTarget space and used as world space IK target rotation
ik.solver.IKRotation = weaponAimTarget.rotation * handRotRelToWeapon;