Unity中简单实现IK(Generic Inverse Kinematics)算法

1.原理解释:

待补充。。。

2.简单实现Demo:

1.搭建简单场景:

2.实现代码:

算法实现分析:

待补充。。。

主要实现代码如下:

cs 复制代码
 void SolveTwoBoneIK()
 {
     Vector3 targetPosition = target.position;
     Vector3 rootPosition = rootBone.position;

     float targetDistance = Vector3.Distance(rootPosition, targetPosition);
     float chainLength = upperLength + lowerLength;

     if (targetDistance >= chainLength)
     {
         Vector3 direction = (targetPosition - rootPosition).normalized;
         middleBone.position = rootPosition + direction * upperLength;
         endBone.position = rootPosition + direction * chainLength;
         return;
     }

     // 使用余弦定理计算关节角度
     // cos(angle) = (a² + b² - c²) / (2ab)

     float upperSqr = upperLength * upperLength;
     float lowerSqr = lowerLength * lowerLength;
     float targetSqr = targetDistance * targetDistance;

     float upperAngle = Mathf.Acos(
         Mathf.Clamp(
             (upperSqr + targetSqr - lowerSqr) / (2 * upperLength * targetDistance),
             -1f, 1f
         )
     ) * Mathf.Rad2Deg;

     float middleAngle = Mathf.Acos(
         Mathf.Clamp(
             (upperSqr + lowerSqr - targetSqr) / (2 * upperLength * lowerLength),
             -1f, 1f
         )
     ) * Mathf.Rad2Deg;

     Vector3 toBendDirection = Vector3.zero;
     if (poleTarget != null)
     {
         Vector3 toTargetPos = targetPosition - rootPosition;
         Vector3 toPole = poleTarget.position - rootPosition;
         toBendDirection = Vector3.ProjectOnPlane(toPole, toTargetPos).normalized;
     }
     else
     {
         toBendDirection = Vector3.Cross(
             targetPosition - rootPosition,
             Vector3.up
         ).normalized;
     }

     // 5. 应用旋转
     Vector3 toTarget = (targetPosition - rootPosition).normalized;

     Quaternion upperRotation = Quaternion.LookRotation(toTarget, toBendDirection);
     upperRotation *= Quaternion.Euler(-upperAngle, 0, 0);
     rootBone.rotation = Quaternion.Lerp(rootBone.rotation, upperRotation, weight);

     middleBone.position = rootBone.position + rootBone.forward * upperLength;

     Vector3 toEnd = (targetPosition - middleBone.position).normalized;
     middleBone.rotation = Quaternion.Lerp(
         middleBone.rotation,
         Quaternion.LookRotation(toEnd, toBendDirection),
         weight
     );

     endBone.position = Vector3.Lerp(
         endBone.position,
         targetPosition,
         weight
     );
 }

实现结果:

参考链接:

Unity 3D - Generic Inverse Kinematics (IK) - Open Source - Github (youtube.com)

C# Inverse Kinematics in Unity 🎓 (youtube.com)

https://www.youtube.com/watch?v=qqOAzn05fvk

相关推荐
2501_940041741 分钟前
AI创建小游戏指令词
人工智能·游戏·prompt
不绝1912 小时前
导航系统/NavMeshAgent组件
unity
mxwin4 小时前
Unity Shader 屏幕空间 UVScreen Space UV 完全指南
unity·游戏引擎·uv
LF男男7 小时前
TouchManager
unity·c#
programhelp_8 小时前
Roblox OA 真题分享(2026 最新)|在线游戏模拟题 + BQ,全程像在玩经营游戏
游戏
mxwin8 小时前
Unity Shader 径向模糊与径向 UV 变形速度感 · 冲击波效果完全指南
unity·游戏引擎·shader·uv
weixin_423995009 小时前
unity 微信开发小游戏,网络资源获取数据
unity·游戏引擎
Yasin Chen9 小时前
Unity TMP_SDF 分析(五)片元着色器
unity·游戏引擎·着色器
mxwin10 小时前
Unity Shader Texture Bombing用随机旋转与偏移的多次采样,打破大地形纹理的
unity·游戏引擎
代数狂人10 小时前
《深入浅出Godot 4与C# 3D游戏开发》第二章:编辑器导航
3d·编辑器·游戏引擎·godot