using asap.core; using cfg; using RootMotion.FinalIK; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public enum EHandState { None, Idle, Hold, Fishing, Catch, Release, End } public class HandController : MonoBehaviour { // private Animator anim_hand; //[HideInInspector] //public Animator anim_rightElbowDirection; HandPoser poser_l; HandPoser poser_r; private FullBodyBipedIK _bodyIK; private Transform _leftElbowDireciton, _rightElbowDireciton, _rightHandTargets, _leftHandTargets, Shoulder; private bool _isRightHandFollowTarget; private bool _isLeftHandFollowTarget; bool _canShowHand = true; // 默认显示手部 public EHandState EHandState { get; private set; } #region State Method bool isInit; public void ChangeHandState(EHandState state) { } public void OnStateEnter(EHandState state) { } public void OnStateExit() { } public void SetActive(bool value) { if (_canShowHand) { gameObject.SetActive(value); } else { gameObject.SetActive(false); } } #endregion #region Component Method void Awake() { Init(); } private void Init() { if (isInit) { return; } isInit = true; Shoulder = transform.Find("HandComponent/Shoulder"); // anim_hand = transform.Find("Hand").GetComponent(); _rightElbowDireciton = transform.Find("HandComponent/Elbow/RightElbowDirection"); _leftElbowDireciton = transform.Find("HandComponent/Elbow/LeftElbowDirection"); //anim_rightElbowDirection = transform.Find("RightElbowDirection").GetComponent(); _bodyIK = GetComponentInChildren(); var posers = GetComponentsInChildren(); if (posers.Length == 2) { poser_l = posers[0]; poser_r = posers[1]; } else { Debug.LogWarning("HandPoser not found: " + posers.Length); } _bodyIK.solver.rightArmChain.bendConstraint.bendGoal = _rightElbowDireciton; _bodyIK.solver.leftArmChain.bendConstraint.bendGoal = _leftElbowDireciton; } private void OnEnable() { SetShoulderRota(Vector3.zero); } public void SetHandTargets(Transform rightHand, Transform LeftHand, Transform body) { Init(); _leftHandTargets = LeftHand; _rightHandTargets = rightHand; _bodyIK.solver.leftHandEffector.target = _leftHandTargets; _bodyIK.solver.rightHandEffector.target = _rightHandTargets; //_bodyIK.solver.bodyEffector.target = body; if (poser_l != null) { poser_l.poseRoot = _leftHandTargets; } if (poser_r != null) { poser_r.poseRoot = _rightHandTargets; } SetAllHandIkTarget(1f); } private void SetAllHandIkTarget(float weight) { SetHandIKWeight(_bodyIK.solver.leftHandEffector, weight); SetHandIKWeight(_bodyIK.solver.rightHandEffector, weight); } private void SetHandIKWeight(IKEffector effector, float weight) { effector.positionWeight = weight; effector.rotationWeight = weight; } public void SetShoulderRota(Vector3 vector) { Quaternion quaternion = Quaternion.Euler(vector); Shoulder.localRotation = quaternion; } #endregion }