反向动力学 matlab IK,Ik反向动力学

一般我们都是通过父节点来调动子节点运动,而在实际开发过程中,有时候我们需要子节点带动父节点进行相应的动作。这种情况就是反向动力学。其实在国内的游戏中很少看见IK动画,一般而言国外的游戏较多。例如刺客信条,手部带动身体的爬行。

通俗讲:使用场景中的各种物体来控制和影响角色身体部位的运动

案例

首先我们先打开IK并设置权重为1

3ce78bedc64e

Paste_Image.png

然后我们创建一个 Avatar Mask设置其固定骨骼

3ce78bedc64e

Paste_Image.png随后我们创建脚本Iktext 并写入一下代码,

public class Iktext : MonoBehaviour

{

public Animator ani;

public Transform sphere;

private GameObject shoulei;//游戏对象

public Transform shou;//手的位置

private Rigidbody body;

private float speed;//速度

public Transform cube;

void Start ()

{

speed = 500;

}

// Update is called once per frame

void Update () {

if (Input.GetKey(KeyCode.W))

{

sphere.transform.Translate(new Vector3(0, 0,0.1f));

}

if (Input.GetKey(KeyCode.S))

{

sphere.transform.Translate(new Vector3(0, 0, -0.1f));

}

if (Input.GetKey(KeyCode.A))

{

sphere.transform.Translate(new Vector3(-0.1f, 0, 0));

}

if (Input.GetKey(KeyCode.D))

{

sphere.transform.Translate(new Vector3(0.1f, 0, 0));

}

if (Input.GetKey(KeyCode.J))

{

sphere.transform.Translate(new Vector3(0, 0.1f, 0));

}

if (Input.GetKey(KeyCode.N))

{

sphere.transform.Translate(new Vector3(0, -0.1f, 0));

}

if (Input.GetKeyDown( KeyCode.Space))

{

ani.Play("Throw 0");

}

}

void OnAnimatorIK(int layerIndex)//IK反向动力学

{

ani.SetLookAtWeight(1,1,1,1);//设置头部,身体,手,脚都跟着游戏对象动

if (sphere)

{

ani.SetLookAtPosition(sphere.position);//看向Ik视觉的位置

}

if (cube)

{

ani.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);//设置IK(反向)位置的权重(设置左脚,设置权重为1)

ani.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);//设置IK(反向)旋转的权重(设置左手,设置权重为1)

ani.SetIKPosition(AvatarIKGoal.LeftFoot, cube.transform.position);//设置IK (反向)位置(设置左脚位置,按照Cube的位置来设置动画人物的左脚的位置)

ani.SetIKRotation(AvatarIKGoal.RightHand, cube.transform.rotation);//设置IK (反向)旋转(旋转左手,按照Cube的旋转来设置动画人物的左手旋转)

}

}

void NewEvent()//动画事件系统

{

shoulei= GameObject.CreatePrimitive(PrimitiveType.Sphere);

shoulei.AddComponent();

shoulei.transform.localScale=new Vector3(0.1f,0.1f,0.1f);

shoulei.AddComponent();

shoulei.transform.position = shou.position;

body=shoulei.GetComponent();

body.AddForce((sphere.transform.position-shou.transform.position)*Time.deltaTime*speed,ForceMode.Impulse);

}

}

随后我们给其赋值

3ce78bedc64e

Paste_Image.png我们在将投球的状态机做好让我们给其赋动画的值和骨骼。这样我们大概就完成了 运行看看效果吧

IK反向动力效果

3ce78bedc64e

IK.gif

事件系统效果

3ce78bedc64e

SJ.gif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值