Search Unity

Can't figure out IK aiming

Discussion in 'Scripting' started by MikeyJY, Apr 19, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I'm trying to make a C# for enemy to aim at player, but I don't know how to rotate the weapon:
    In hierarchy, the weapon is inside the right hand. I tried the basic IK example at the unity documentation page.
    I modified it and I have this:
    Code (CSharp):
    1. private void OnAnimatorIK()
    2.     {
    3.  
    4.         animator.SetLookAtWeight(1);
    5.         animator.SetLookAtPosition(PlayerTarget.position);
    6.      
    7.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    8.         animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    9.         animator.SetIKPosition(AvatarIKGoal.RightHand, PlayerTarget.position);
    10.         animator.SetIKRotation(AvatarIKGoal.RightHand, PlayerTarget.rotation);
    11.      
    12.     }
    The PlayerTarget is inside Player, but the gun isn't rotating at the player and it is modified by player rotation. If I turn around 180 the enemy's arm is also rotating the arm.
    I tried to put the target outside the Player and make it follow player by script, but without rotation, only position. It isn't also working.

    Do you have an IK enemy aiming a gun at player script?

    Thanks!
     
  2. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Ok, but how exactly implement IK and rotation to make an enemey aiming?
     
  4. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Oh, sorry, my bad. I misread your post. But you can check out the tutorial, hopefully it helps a little. I'm too noob to give advice, sorry lol