SHARE
TWEET

Untitled

a guest Feb 14th, 2020 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public LayerMask ground;
  2.  
  3.     public float pelvisHeightOffset;
  4.     public float IKGroundCheckDistance;
  5.  
  6.     Animator animator;
  7.     CharacterController characterController;
  8.  
  9.     Vector3 leftFootIKPosition;
  10.     Vector3 rightFootIKPosition;
  11.     Quaternion leftFootIKRotation;
  12.     Quaternion rightFootIKRotation;
  13.  
  14.     void Start()
  15.     {
  16.         characterController = GetComponent<CharacterController>();
  17.         animator = GetComponent<Animator>();
  18.     }
  19.  
  20.     void Update()
  21.     {
  22.         FeetPositionSolver(animator.GetBoneTransform(HumanBodyBones.RightFoot).position + Vector3.up, ref rightFootIKPosition, ref rightFootIKRotation);
  23.         FeetPositionSolver(animator.GetBoneTransform(HumanBodyBones.LeftFoot).position + Vector3.up, ref leftFootIKPosition, ref leftFootIKRotation);
  24.     }
  25.  
  26.     private void FeetPositionSolver(Vector3 fromSkyPosition, ref Vector3 feetIKPosition, ref Quaternion feetIkRotation)
  27.     {
  28.         RaycastHit feetOutHit;
  29.  
  30.         if (Physics.Raycast(fromSkyPosition, Vector3.down, out feetOutHit, IKGroundCheckDistance, ground, QueryTriggerInteraction.Ignore))
  31.         {
  32.             feetIKPosition = fromSkyPosition;
  33.             feetIKPosition.y = feetOutHit.point.y + pelvisHeightOffset;
  34.             feetIkRotation = Quaternion.FromToRotation(Vector3.up, feetOutHit.normal) * transform.rotation;
  35.  
  36.             return;
  37.         }
  38.  
  39.         feetIKPosition = Vector3.zero;
  40.     }
  41.  
  42.     float lastFootPositionY;
  43.     float lastFootPositionY1;
  44.  
  45.     private void OnAnimatorIK(int layerIndex)
  46.     {
  47.         animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, animator.GetFloat("LeftFootPlantingRotation"));
  48.         animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, animator.GetFloat("RightFootPlantingRotation"));
  49.         animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
  50.         animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
  51.  
  52.         MoveFeetToIkPoint(AvatarIKGoal.LeftFoot, leftFootIKPosition, leftFootIKRotation, ref lastFootPositionY);
  53.         MoveFeetToIkPoint(AvatarIKGoal.RightFoot, rightFootIKPosition, rightFootIKRotation, ref lastFootPositionY1);
  54.  
  55.         MovePelvisHeight();
  56.     }
  57.  
  58.     public float feetIKPositionSpeed;
  59.  
  60.     void MoveFeetToIkPoint(AvatarIKGoal foot, Vector3 positionIKHolder, Quaternion rotationIKHolder, ref float lastFootPositionY)
  61.     {
  62.         Vector3 targetIKPosition = animator.GetIKPosition(foot);
  63.  
  64.         if (positionIKHolder != Vector3.zero)
  65.         {
  66.             targetIKPosition = transform.InverseTransformPoint(targetIKPosition);
  67.             positionIKHolder = transform.InverseTransformPoint(positionIKHolder);
  68.  
  69.             float yVariable = Mathf.Lerp(lastFootPositionY, positionIKHolder.y, feetIKPositionSpeed);
  70.  
  71.             targetIKPosition.y += yVariable;
  72.  
  73.             lastFootPositionY = yVariable;
  74.  
  75.             targetIKPosition = transform.TransformPoint(targetIKPosition);
  76.  
  77.             animator.SetIKRotation(foot, rotationIKHolder);
  78.         }
  79.  
  80.         animator.SetIKPosition(foot, targetIKPosition);
  81.     }
  82.  
  83.     public float lastPelvisPositionY;
  84.     public float pelvisDampSpeed;
  85.  
  86.     private void MovePelvisHeight()
  87.     {
  88.         if (rightFootIKPosition == Vector3.zero || leftFootIKPosition == Vector3.zero || lastPelvisPositionY == 0)
  89.         {
  90.             lastPelvisPositionY = animator.bodyPosition.y;
  91.             return;
  92.         }
  93.  
  94.         float lOffset = leftFootIKPosition.y - transform.position.y;
  95.         float rOffset = rightFootIKPosition.y - transform.position.y;
  96.  
  97.         float totalOffset = (lOffset < rOffset) ? lOffset : rOffset;
  98.  
  99.         if (Mathf.Abs(totalOffset) > 0.7f)
  100.         {
  101.             lastPelvisPositionY = animator.bodyPosition.y;
  102.             return;
  103.         }
  104.  
  105.         Vector3 newPelvisPosition = animator.bodyPosition + Vector3.up * totalOffset;
  106.  
  107.         newPelvisPosition.y = Mathf.Lerp(lastPelvisPositionY, newPelvisPosition.y, pelvisDampSpeed);
  108.  
  109.         animator.bodyPosition = newPelvisPosition;
  110.  
  111.         lastPelvisPositionY = animator.bodyPosition.y;
  112.     }
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