Script to point the character's arms to where he is aiming (IK Aim)

I am new to animations. After combining my aiming animation with the basic ones (through the creation of a second layer), now I am stuck because I cannot direct the character’s arms to the target point I want to aim at.
I also tried to use this script which was fine for the bust but if I apply it to my shoulders or arms when I start it I find all the bones twisted and in bad shape, and even if I then fix them with the offset, however, the animation is ruined ( even if in this way the arms follow the target).
I would instead like to take the current animation and by script change the rotation up and down towards the target.

  public Transform Target;
    public Vector3 OffsetArmLeft;
    public Vector3 OffsetArmRight;

    Animator anim;
    Transform leftHarm;
    Transform rightHarm;

void Start()
    {
        anim = GetComponent<Animator>();
        leftHarm = anim.GetBoneTransform(HumanBodyBones.LeftShoulder);
        rightHarm = anim.GetBoneTransform(HumanBodyBones.RightShoulder);
    }

void LateUpdate()
    {
     leftHarm.LookAt(Target.position);
     leftHarm.rotation = leftHarm.rotation * Quaternion.Euler(OffsetArmLeft);

      rightHarm.LookAt(Target.position);
      rightHarm.rotation = rightHarm.rotation * Quaternion.Euler(OffsetArmRight);
}

Take a look at the example scripts here:

https://blogs.unity3d.com/2018/08/27/animation-c-jobs/

Some of them will likely already solve this, but if not it will give you where you need to start.

My problem is that I know what I want to do (a simple LookAt) but it doesn’t work with my arms. I just tried again and used the head … my head character follows the target and everything works as with the bust. However, if I call the shoulders or the high arm (to be able to target the target), when I insert LookAt I find all the arms twisted. Maybe it’s a problem with the skeleton but the animation (taken on mixamo) works perfectly, and then if I use the offset I move the limb of the arms that I want so I don’t understand.
Maybe LookAt is not meant to be used with the arms? but I don’t think

Right but did you actually try using any of the animation c# example scripts in the link I posted? What was wrong when you tried those, they are good examples of how to control limbs and you should be able to get an idea of what to do next or at least provide us info with what you are stuck on at that point

You are doing a transform.LookAt which does no IK and that is the problem. The scripts I posted show how to do IK which is what you need.

1 Like

Or you simply set an IK like descripted here:

First of all thank you very much for that list. At the moment I wanted to do a simple lookAt but since it doesn’t work, I think I could try using something like Two Bone Ik (I saw that it moves an entire arm so I think it is possible to use it to raise / lower the arm according to the height of the target). I don’t know if it’s the right solution but it could be.
However I have not understood well how to implement this function in unity (sorry but as said before I am only now entering the world of animations).

This I had already tried to use it before writing the post.
I did a quick test and entered this code (which will surely be wrong) but the hand does not move. I thought it called the IK movement of the limbs but the hand of my character does not change movement.

 void OnAnimatorIK()
{      animator.SetIKPosition(AvatarIKGoal.RightHand,target.position);
}

I wanted to see if in this way the position of the hand changed according to that of the target but did not move.
Could you give me an example of how to move the arm in relation to the target ??

You have to enable the IK pass in your animation controller (see IK Pass checkbox):

Then you must set the weights and the rotation too:

 animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
 animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
 animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
 animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);

Here is a little video where I tried the method described in above Unity documentation:
odzupg

1 Like