- Home /
Problem has been fixed, by creating animations for each gun/weapon
Character IK hand position and bone transform problem.
Hello.
I've had created and rigged a character in Blender.
My problem is I have a gun that is animated with reloading animation,aiming,walking,sprinting,idle ( animations created in Unity ) and character ( animated in Blender -> only legs are animated -> walking,idle,sprinting,crouching) and i want to make it so the character holds the gun with his hand and not the transform of his hand ( the bone transform)
Is there a way how to do this ? I had tried adding a bone on the hand and then positioning the hand on the green cube position. (hand didn't move) I had also tried changing the mesh of the hand ( didn't worked )
This is how he holds it.
and this is how i want him to hold the gun.
I have gun with gameobjects that are supposed to tell which hand is supposed to be on that location. Blue cube - Left hand Green cube - Right hand ( Hand is positioned at its transform position )
Hands are controlled by IK.
This is my script.
protected Animator animator;
public bool ikActive = false;
public Transform rightHandObj = null;
public Transform lookObj = null;
public Transform left = null;
void Start()
{
animator = GetComponent<Animator>();
}
//a callback for calculating IK
void OnAnimatorIK()
{
if (animator)
{
//if the IK is active, set the position and rotation directly to the goal.
if (ikActive)
{
// Set the look target position, if one has been assigned
if (lookObj != null)
{
animator.SetLookAtWeight(1,1);
animator.SetLookAtPosition(lookObj.position);
}
// Set the right hand target position and rotation, if one has been assigned
if (rightHandObj != null)
{
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
}
if (left != null)
{
animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
animator.SetIKPosition(AvatarIKGoal.LeftHand, left.position);
animator.SetIKRotation(AvatarIKGoal.LeftHand, left.rotation);
}
}
//if the IK is not active, set the position and rotation of the hand and head back to the original position
else
{
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);
animator.SetLookAtWeight(0,0);
animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 0);
animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 0);
}
}
}
}
Anybody knows how to "fix" this ?
Have you tried moving the I$$anonymous$$Goal of the hand? What's the I$$anonymous$$Goal parented to? I iguess it's parented to the gun handle. In game mode, position the I$$anonymous$$ goal well enough to have the hand in the desired position. Then copy the transform values of the goal. Exit play mode and paste those values in the same place from where you copied them. Unless I misunderstood what you're trying to do...
Answer by theANMATOR2b · Jan 26, 2018 at 07:32 PM
The only issue is the position of the gun in relation to the hand? Im not a coder but I think there is something related to parent or child transform offset. That might help enough to point towards the correct code block.