SHARE
TWEET

AimIK

a guest May 1st, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //add IK to the first layer in the Animator controller and add a second layer with IK enabled to control the arms
  5.  
  6. public class AimIK : MonoBehaviour
  7. {
  8.     [Header ("IK Transform References")]
  9.  
  10.     public Transform ikReference = null;
  11.     public Transform leftHandle = null;
  12.     public Transform rightHandle = null;
  13.  
  14.     private Vector3 target;
  15.     private Animator animator;
  16.  
  17.     [Header("IK Weight Values")]
  18.     [Range(0,1)]
  19.     public float aimWeight;
  20.     public float aimAdjustment, bodyWeight, headWeight, eyesWeight, clampWeight;
  21.  
  22.     // Use this for initialization
  23.     void Start ()
  24.     {
  25.         animator = GetComponent<Animator> ();
  26.  
  27.  
  28.  
  29.     }
  30.  
  31.     void OnAnimatorIK (int layerIndex)
  32.     {
  33.                 target = ikReference.position;
  34.                 target.y = target.y + aimAdjustment;
  35.                 animator.SetLookAtPosition (target);
  36.                 animator.SetLookAtWeight (aimWeight, bodyWeight, headWeight, eyesWeight, clampWeight);
  37.  
  38.  
  39.             if (leftHandle != null) {
  40.                 animator.SetIKPosition (AvatarIKGoal.LeftHand, leftHandle.transform.position);
  41.                 animator.SetIKRotation (AvatarIKGoal.LeftHand, leftHandle.transform.rotation);
  42.                 animator.SetIKPositionWeight (AvatarIKGoal.LeftHand, aimWeight);
  43.                 animator.SetIKRotationWeight (AvatarIKGoal.LeftHand, aimWeight);
  44.             }
  45.  
  46.             if (rightHandle != null) {
  47.                 animator.SetIKPosition (AvatarIKGoal.RightHand, rightHandle.transform.position);
  48.                 animator.SetIKRotation (AvatarIKGoal.RightHand, rightHandle.transform.rotation);
  49.                 animator.SetIKPositionWeight (AvatarIKGoal.RightHand, aimWeight);
  50.                 animator.SetIKRotationWeight (AvatarIKGoal.RightHand, aimWeight);
  51.             }
  52.         }
  53.  
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top