Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class IKControl : MonoBehaviour {
  6.  
  7. Animator _animator;
  8.  
  9. public float transitionTime = 0.3f;
  10.  
  11. public Transform lookAtDestination;
  12. public Transform rightHandDestination;
  13. public Transform leftHandDestination;
  14. public Transform rightFootDestination;
  15. public Transform leftFootDestination;
  16.  
  17. Vector3 lookAtPoint;
  18. Vector3 rightHandPoint;
  19. Vector3 leftHandPoint;
  20. Vector3 rightFootPoint;
  21. Vector3 leftFootPoint;
  22.  
  23. Quaternion rightHandRotation;
  24. Quaternion leftHandRotation;
  25. Quaternion rightFootRotation;
  26. Quaternion leftFootRotation;
  27.  
  28. float _lookAtWeight;
  29. float _rightHandWeight;
  30. float _leftHandWeight;
  31. float _rightFootWeight;
  32. float _leftFootWeight;
  33.  
  34. void Awake()
  35. {
  36. _animator = GetComponent<Animator>();
  37. }
  38.  
  39. void Update()
  40. {
  41. if(lookAtDestination != null) { lookAtPoint = lookAtDestination.position; }
  42. UpdatePositionAndRotation(ref rightHandPoint, ref rightHandRotation, rightHandDestination);
  43. UpdatePositionAndRotation(ref leftHandPoint, ref leftHandRotation, leftHandDestination);
  44. UpdatePositionAndRotation(ref rightFootPoint, ref rightFootRotation, rightFootDestination);
  45. UpdatePositionAndRotation(ref leftFootPoint, ref leftFootRotation, leftFootDestination);
  46.  
  47. UpdateWeight(ref _lookAtWeight, lookAtDestination);
  48. UpdateWeight(ref _rightHandWeight, rightHandDestination);
  49. UpdateWeight(ref _leftHandWeight, leftHandDestination);
  50. UpdateWeight(ref _rightFootWeight, rightFootDestination);
  51. UpdateWeight(ref _leftFootWeight, leftFootDestination);
  52. }
  53.  
  54. void OnAnimatorIK(int layer)
  55. {
  56. if(lookAtDestination != null) { _animator.SetLookAtPosition(lookAtDestination.position); }
  57. _animator.SetLookAtWeight(_lookAtWeight);
  58.  
  59. SetIKParams(AvatarIKGoal.RightHand, rightHandPoint, rightHandRotation, _rightHandWeight);
  60. SetIKParams(AvatarIKGoal.LeftHand, leftHandPoint, leftHandRotation, _leftHandWeight);
  61. SetIKParams(AvatarIKGoal.RightFoot, rightFootPoint, rightFootRotation, _rightFootWeight);
  62. SetIKParams(AvatarIKGoal.LeftFoot, leftFootPoint, leftFootRotation, _leftFootWeight);
  63. }
  64.  
  65. void UpdateWeight(ref float weight, Transform destination)
  66. {
  67. weight = Mathf.Lerp(weight, destination ? 1 : 0, Time.deltaTime / transitionTime);
  68. }
  69.  
  70. void UpdatePositionAndRotation(ref Vector3 position, ref Quaternion rotation, Transform destination)
  71. {
  72. if(destination != null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement