Question by
Gameatro · May 15, 2020 at 04:28 PM ·
animationikinverse kinematicweapon system
The weapon rotating slower than the torso of character when using IK to attach weapon to hands
I am trying to use IK to attach weapon to player's hand. This is my IK setup: Here the Right Hand Target is at the Trigger handle, while Left Hand Target is in the front. Here is my IKHandler code:
public class IKHandler : MonoBehaviour
{
Animator anim;
Vector3 lookPos;
Vector3 IK_lookPos;
Vector3 targetPos;
PlayerContorller pc;
public float lerpRate = 15;
public float updateLookPosThreshold = 2;
public float lookWeight = 1;
public float bodyWeight = .9f;
public float headWeight = 1;
public float clampWeight = 1;
public float rightHandWeight = 1;
public float leftHandWeight = 1;
public Transform rightHandTarget;
public Transform rightElbowTarget;
public Transform leftHandTarget;
public Transform leftElbowTarget;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
pc = GetComponent<PlayerContorller>();
}
void OnAnimatorIK()
{
anim.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKPosition(AvatarIKGoal.RightHand, rightHandTarget.position);
anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHandTarget.position);
anim.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
anim.SetIKRotation(AvatarIKGoal.RightHand, rightHandTarget.rotation);
anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHandTarget.rotation);
anim.SetIKHintPositionWeight(AvatarIKHint.RightElbow, rightHandWeight);
anim.SetIKHintPositionWeight(AvatarIKHint.LeftElbow, leftHandWeight);
anim.SetIKHintPosition(AvatarIKHint.RightElbow, rightElbowTarget.position);
anim.SetIKHintPosition(AvatarIKHint.LeftElbow, leftElbowTarget.position);
lookPos = pc.lookPos;
float distanceFromPlayer = Vector3.Distance(lookPos, transform.position);
if(distanceFromPlayer > updateLookPosThreshold)
{
targetPos = lookPos;
}
IK_lookPos = Vector3.Lerp(IK_lookPos, targetPos, Time.deltaTime * lerpRate);
anim.SetLookAtWeight(lookWeight, bodyWeight, headWeight, headWeight, clampWeight);
anim.SetLookAtPosition(IK_lookPos);
}
Here is what is happening: video The character's torso rotates before the gun rotates. Also, in the beginning, the torso is rotated towards left. What is the reason for this? What am I doing wrong? I am new to IK.
annotation-2020-05-15-214748.png
(14.2 kB)
Comment