Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class IKControl : MonoBehaviour {
- Animator _animator;
- public float transitionTime = 0.3f;
- public Transform lookAtDestination;
- public Transform rightHandDestination;
- public Transform leftHandDestination;
- public Transform rightFootDestination;
- public Transform leftFootDestination;
- Vector3 lookAtPoint;
- Vector3 rightHandPoint;
- Vector3 leftHandPoint;
- Vector3 rightFootPoint;
- Vector3 leftFootPoint;
- Quaternion rightHandRotation;
- Quaternion leftHandRotation;
- Quaternion rightFootRotation;
- Quaternion leftFootRotation;
- float _lookAtWeight;
- float _rightHandWeight;
- float _leftHandWeight;
- float _rightFootWeight;
- float _leftFootWeight;
- void Awake()
- {
- _animator = GetComponent<Animator>();
- }
- void Update()
- {
- if(lookAtDestination != null) { lookAtPoint = lookAtDestination.position; }
- UpdatePositionAndRotation(ref rightHandPoint, ref rightHandRotation, rightHandDestination);
- UpdatePositionAndRotation(ref leftHandPoint, ref leftHandRotation, leftHandDestination);
- UpdatePositionAndRotation(ref rightFootPoint, ref rightFootRotation, rightFootDestination);
- UpdatePositionAndRotation(ref leftFootPoint, ref leftFootRotation, leftFootDestination);
- UpdateWeight(ref _lookAtWeight, lookAtDestination);
- UpdateWeight(ref _rightHandWeight, rightHandDestination);
- UpdateWeight(ref _leftHandWeight, leftHandDestination);
- UpdateWeight(ref _rightFootWeight, rightFootDestination);
- UpdateWeight(ref _leftFootWeight, leftFootDestination);
- }
- void OnAnimatorIK(int layer)
- {
- if(lookAtDestination != null) { _animator.SetLookAtPosition(lookAtDestination.position); }
- _animator.SetLookAtWeight(_lookAtWeight);
- SetIKParams(AvatarIKGoal.RightHand, rightHandPoint, rightHandRotation, _rightHandWeight);
- SetIKParams(AvatarIKGoal.LeftHand, leftHandPoint, leftHandRotation, _leftHandWeight);
- SetIKParams(AvatarIKGoal.RightFoot, rightFootPoint, rightFootRotation, _rightFootWeight);
- SetIKParams(AvatarIKGoal.LeftFoot, leftFootPoint, leftFootRotation, _leftFootWeight);
- }
- void UpdateWeight(ref float weight, Transform destination)
- {
- weight = Mathf.Lerp(weight, destination ? 1 : 0, Time.deltaTime / transitionTime);
- }
- void UpdatePositionAndRotation(ref Vector3 position, ref Quaternion rotation, Transform destination)
- {
- if(destination != null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement