1 year ago
#281733
SpookyTheCat
Unity3D - Inverse Kinematics - Turn off smoothly
I'm working on the Animations of a project and I'm using Unity's inbuilt inverse kinematics
I have a NPC which has certain animations like "walk", "grab", "insert". Now once the NPC is in the grab or insert animation it should grab towards the current object. For this, I'm using inverse kinematics.
I have a parameter in my Animator called "IKWeight". To make the NPC's hand smoothly move towards the object and back to the original animation position, I use this code:
void OnAnimatorIK()
{
if (_objectAnimator)
{
if (ikActive)
{
// Set the right hand target position and rotation and the look target position
if (currentObject != null) //This is the Object where the NPC should reach
{
_objectAnimator.SetFloat("IKWeight", 1.0f, 0.1f, Time.deltaTime * 0.2f); // to make the transition smooth
_objectAnimator.SetLookAtWeight(_objectAnimator.GetFloat("IKWeight"), 1, 0, 0);
_objectAnimator.SetLookAtPosition(currentObject.position);
_objectAnimator.SetIKPositionWeight(AvatarIKGoal.RightHand, _objectAnimator.GetFloat("IKWeight"));
_objectAnimator.SetIKPosition(AvatarIKGoal.RightHand, currentObject.position);
}
} //the bit above works fine!
//if the IK is not active, set the position and rotation of the hand and head back to the original position
else
{
_objectAnimator.SetFloat("IKWeight", 0.0f, 0.1f, Time.deltaTime * 0.2f);
_objectAnimator.SetIKPositionWeight(AvatarIKGoal.RightHand, _objectAnimator.GetFloat("IKWeight"));
_objectAnimator.SetLookAtWeight(_objectAnimator.GetFloat("IKWeight"), _objectAnimator.GetFloat("IKWeight"));
} //This does not work fine. It instantly jumps back to the original position, there is no smooth transition.
}
}
I turn the bool ikActive on or off, so i.e. it's on as the NPC is ready to grab, and off after the grab is done and the object is attached to the hand.
My NPC goes into the grabbing animation and without IK it just reaches straight in front of it. But with IK it should reach towards the object and then, after grabbing the object, return to where the animation was if IK was never used. After that it transitions into the walk animation which is why it should have been set back to the original position before then.
However, what happens is, that when grabbing the object (ikActive == true), the hand smoothly moves towards the object (which is what I want). However, once the object is attched and ikActive is set to false, it does not return to the original position smoothly. Instead, it just snaps/jumps to the original position. There is not smooth transition from the IK Position to the original animation position
Here is a video showing the problem.
Now, my question is: Why does it not return to the original position smoothly? Does anyone see anything wrong with the code?
Thanks in advance!
unity-game-engine
animation
inverse-kinematics
0 Answers
Your Answer