Pimeko

Untitled

May 19th, 2021
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using Sirenix.OdinInspector;
  5. using UnityEngine;
  6.  
  7. public class HandsController : MonoBehaviour
  8. {
  9.     [SerializeField]
  10.     Transform leftHandIKTarget, rightHandIKTarget;
  11.  
  12.     Animator animator;
  13.     Animator CurrentAnimator => transform.CachedComponent(ref animator);
  14.  
  15.     Vector3 currentLeft, currentRight;
  16.     Quaternion currentQLeft, currentQRight;
  17.  
  18.     float speed = 10;
  19.  
  20.     bool reachIK;
  21.  
  22.     void Start()
  23.     {
  24.         reachIK = true;
  25.     }
  26.  
  27.     void OnAnimatorIK(int _)
  28.     {
  29.         SetIk(AvatarIKGoal.LeftHand, leftHandIKTarget, ref currentLeft, ref currentQLeft, reachIK ? 1 : 0);
  30.         SetIk(AvatarIKGoal.RightHand, rightHandIKTarget, ref currentRight, ref currentQRight, reachIK ? 1 : 0);
  31.     }
  32.  
  33.     void SetIk(AvatarIKGoal iKGoal, Transform to, ref Vector3 current, ref Quaternion currentQ, float weight = 1)
  34.     {
  35.         current = Vector3.Slerp(current, to.position, Time.deltaTime * speed);
  36.         CurrentAnimator.SetIKPositionWeight(iKGoal, weight);
  37.         CurrentAnimator.SetIKPosition(iKGoal, current);
  38.  
  39.         currentQ = Quaternion.Slerp(currentQ, to.rotation, Time.deltaTime * speed);
  40.         CurrentAnimator.SetIKRotationWeight(iKGoal, weight);
  41.         CurrentAnimator.SetIKRotation(iKGoal, currentQ);
  42.     }
  43.  
  44.     public void OnFinish()
  45.     {
  46.         reachIK = false;
  47.  
  48.         CurrentAnimator.SetBool("dance", true);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment