- Home /
IK Targets are not working
I want my left hand's position to fit properly with the gun but none of the IK Targets are working. Here's my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tets : MonoBehaviour
{
[System.Serializable]
private class IKTransforms {
[SerializeField] private Transform leftHandIK;
[SerializeField] private Transform rightHandIK;
[SerializeField] private Transform lookAtIK;
public Transform LeftHandIK { get => leftHandIK; private set => leftHandIK = value; }
public Transform RightHandIK { get => rightHandIK; private set => rightHandIK = value; }
public Transform LookAtIK { get => lookAtIK; private set => lookAtIK = value; }
}
public Animator animator;
[SerializeField] private IKTransforms ikTransforms;
// Update is called once per frame
void Update()
{
if (ikTransforms.LeftHandIK && ikTransforms.RightHandIK && ikTransforms.LookAtIK) {
if (ikTransforms.LeftHandIK) {
animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1f);
animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1f);
animator.SetIKPosition(AvatarIKGoal.LeftHand, ikTransforms.LeftHandIK.position);
animator.SetIKRotation(AvatarIKGoal.LeftHand, ikTransforms.LeftHandIK.rotation);
}
if (ikTransforms.RightHandIK) {
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1f);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1f);
animator.SetIKPosition(AvatarIKGoal.RightHand, ikTransforms.RightHandIK.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, ikTransforms.RightHandIK.rotation);
}
if (ikTransforms.LookAtIK) {
animator.SetLookAtWeight(1f);
animator.SetLookAtPosition(ikTransforms.LookAtIK.position);
}
}
}
}
and Here's my animator controller settings:
Answer by ryanmillerca · Oct 14, 2021 at 03:40 PM
Call your IK code in OnAnimatorIK()
instead of Update()
. More info here if you need it: https://docs.unity3d.com/Manual/InverseKinematics.html
Hey ryanmillerca your solution is working in my test project but not in main project, i have checked if OnAnimatorIK function is not written twice and it's not, I have exact same setup there as well, what other reasons can cause this issue? Looks like OnAnimatorIK functions is not being called as no logs are printing there in my main project. Thanks for your answer.
Definitely use OnAnimatorIK instead of Update. Check the Avatar configuration in your other project and share a screenshot of that one, like the one you have already, in case there's something amiss that's easy to spot. Keep looking for differences between the two projects and I'm sure you'll discover the issue!
I got it, actually the script which contains the OnAnimatorIK method should be sitting on same gameObject on which the Animator lives. I was missing this thing.
Your answer
Follow this Question
Related Questions
Change Idle/Walk player animation permanently on trigger colliders 1 Answer
Error with my strafe animations on player 0 Answers
Unity2d: when animations is working , player can't move. (Code is here). 1 Answer
how to make multiple transition from one state? 1 Answer
Blending animations for a bow with differing charges 1 Answer