0

如何在手指上使用逆运动学(带旋转)?您好,希望您能帮我解决这个问题!我正在尝试使用手指的逆运动学,我只知道目标的旋转(来自传感器)和手腕中的另一个,在这个例子中我只有两个骨头(远端,中间),我被引导以 Dogzer 为例。

这就是我的示例所做的,但是反向运动学正在相对于position执行,我需要的是它相对于目标 的旋转移动例子

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[ExecuteInEditMode]

 public class InverseKinematics7 : MonoBehaviour {

  public Transform upperArm;
  public Transform forearm;
  public Transform hand;
  public Transform elbow;
  public Transform target;
  [Space(20)]
  public Vector3 uppperArm_OffsetRotation;
  public Vector3 forearm_OffsetRotation;
  public Vector3 hand_OffsetRotation;
  [Space(20)]
  public bool handMatchesTargetRotation = true;
  [Space(20)]
  public bool debug;

  float angle;
  float upperArm_Length;
  float forearm_Length;
  float arm_Length;
  float targetDistance;
  float adyacent;

  // Use this for initialization
  void Start () {

  }

  // Update is called once per frame
  void LateUpdate () {
      if(upperArm != null && forearm != null && hand != null && elbow != null && target != null){
          upperArm.LookAt (target, elbow.position - upperArm.position);
          upperArm.Rotate (uppperArm_OffsetRotation);

          Vector3 cross = Vector3.Cross (elbow.position - upperArm.position, forearm.position - upperArm.position);



          upperArm_Length = Vector3.Distance (upperArm.position, forearm.position);
          forearm_Length =  Vector3.Distance (forearm.position, hand.position);
          arm_Length = upperArm_Length + forearm_Length;
          targetDistance = Vector3.Distance (upperArm.position, target.position);
          targetDistance = Mathf.Min (targetDistance, arm_Length - arm_Length * 0.001f);

          adyacent = ((upperArm_Length * upperArm_Length) - (forearm_Length * forearm_Length) + (targetDistance * targetDistance)) / (2*targetDistance);

          angle = Mathf.Acos (adyacent / upperArm_Length) * Mathf.Rad2Deg;

          upperArm.RotateAround (upperArm.position, cross, -angle);

          forearm.LookAt(target, cross);
          forearm.Rotate (forearm_OffsetRotation);

          if(handMatchesTargetRotation){
              hand.rotation = target.rotation;
              hand.Rotate (hand_OffsetRotation);
          }

          if(debug){
              if (forearm != null && elbow != null) {
                  Debug.DrawLine (forearm.position, elbow.position, Color.blue);
              }

              if (upperArm != null && target != null) {
                  Debug.DrawLine (upperArm.position, target.position, Color.red);
              }
          }

      }

  }

  void OnDrawGizmos(){
      if (debug) {
          if(upperArm != null && elbow != null && hand != null && target != null && elbow != null){
              Gizmos.color = Color.gray;
              Gizmos.DrawLine (upperArm.position, forearm.position);
              Gizmos.DrawLine (forearm.position, hand.position);
              Gizmos.color = Color.red;
              Gizmos.DrawLine (upperArm.position, target.position);
              Gizmos.color = Color.blue;
              Gizmos.DrawLine (forearm.position, elbow.position);
          }
      }
  }

}

4

0 回答 0