一般我們都是通過(guò)父節(jié)點(diǎn)來(lái)調(diào)動(dòng)子節(jié)點(diǎn)運(yùn)動(dòng)房午,而在實(shí)際開發(fā)過(guò)程中矿辽,有時(shí)候我們需要子節(jié)點(diǎn)帶動(dòng)父節(jié)點(diǎn)進(jìn)行相應(yīng)的動(dòng)作。這種情況就是反向動(dòng)力學(xué)郭厌。其實(shí)在國(guó)內(nèi)的游戲中很少看見IK動(dòng)畫袋倔,一般而言國(guó)外的游戲較多。例如刺客信條折柠,手部帶動(dòng)身體的爬行宾娜。
通俗講:使用場(chǎng)景中的各種物體來(lái)控制和影響角色身體部位的運(yùn)動(dòng)
案例
首先我們先打開IK并設(shè)置權(quán)重為1
Paste_Image.png
然后我們創(chuàng)建一個(gè) Avatar Mask設(shè)置其固定骨骼
Paste_Image.png
public class Iktext : MonoBehaviour
{
public Animator ani;
public Transform sphere;
private GameObject shoulei;//游戲?qū)ο? 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反向動(dòng)力學(xué)
{
ani.SetLookAtWeight(1,1,1,1);//設(shè)置頭部扇售,身體前塔,手,腳都跟著游戲?qū)ο髣?dòng)
if (sphere)
{
ani.SetLookAtPosition(sphere.position);//看向Ik視覺(jué)的位置
}
if (cube)
{
ani.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);//設(shè)置IK(反向)位置的權(quán)重(設(shè)置左腳缘眶,設(shè)置權(quán)重為1)
ani.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);//設(shè)置IK(反向)旋轉(zhuǎn)的權(quán)重(設(shè)置左手嘱根,設(shè)置權(quán)重為1)
ani.SetIKPosition(AvatarIKGoal.LeftFoot, cube.transform.position);//設(shè)置IK (反向)位置(設(shè)置左腳位置,按照Cube的位置來(lái)設(shè)置動(dòng)畫人物的左腳的位置)
ani.SetIKRotation(AvatarIKGoal.RightHand, cube.transform.rotation);//設(shè)置IK (反向)旋轉(zhuǎn)(旋轉(zhuǎn)左手巷懈,按照Cube的旋轉(zhuǎn)來(lái)設(shè)置動(dòng)畫人物的左手旋轉(zhuǎn))
}
}
void NewEvent()//動(dòng)畫事件系統(tǒng)
{
shoulei= GameObject.CreatePrimitive(PrimitiveType.Sphere);
shoulei.AddComponent<destory>();
shoulei.transform.localScale=new Vector3(0.1f,0.1f,0.1f);
shoulei.AddComponent<Rigidbody>();
shoulei.transform.position = shou.position;
body=shoulei.GetComponent<Rigidbody>();
body.AddForce((sphere.transform.position-shou.transform.position)*Time.deltaTime*speed,ForceMode.Impulse);
}
}
隨后我們給其賦值
Paste_Image.png
IK反向動(dòng)力效果
IK.gif
事件系統(tǒng)效果
SJ.gif