Unity3D Mecanim高级动画系统详解
2017-03-23 本文已影响325人
TonyWan_AR
一、动画模型的导入

二、模型的设置




三、属性面板相关参数介绍


四、状态机的初步使用






五、添加动画连招必杀技事件



六、动画层应用



七、动画融合树



八、遮罩








九、IK反向动力学
反向动力学:子物体带动父物体运动



IKController关键代码
public class IKController : MonoBehaviour
{
private Animator ani;
public bool isOpenIK = false;
public Transform lookAtObj;
public Transform leftHandObj;
// Use this for initialization
private void Start()
{
ani = this.GetComponent<Animator>();
}
private void OnAnimatorIK()
{
if (ani)
{
if (isOpenIK)
{
if (lookAtObj != null)
{
ani.SetLookAtWeight(1f);
ani.SetLookAtPosition(lookAtObj.position);
}
else
{
ani.SetLookAtWeight(0f);
}
if (leftHandObj != null)
{
ani.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1f);
ani.SetIKRositionWeight(AvatarIKGoal.LeftHand, 1f);
ani.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.position);
ani.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.rotation);
}
else
{
ani.SetIKPositionWeight(AvatarIKGoal.LeftHand, 0);
ani.SetIKRositionWeight(AvatarIKGoal.LeftHand, 0);
}
}
}
}
}

十、运动重定向




关键代码
public class StoneKingController : MonoBehaviour
{
private Animator ani;
AnimatorStateInfo stateInfor;
bool isCanPlaySkill=false;
// Use this for initialization
void Start ()
{
ani = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
if (ani)
{
stateInfor =ani.GetCurrentAnimatorStateInfo(0);
//if (Input.GetKeyDown(KeyCode.W))
// {
// if (!stateInfor.IsName("Walk"))
// {
// ani.SetBool("Walk",true);
// }
// else
// {
// ani.SetBool("Walk",false);
//}
// }
if (Input.GetKeyDown(KeyCode.A))
{
ani.SetTrigger("Attack1");
}
if (Input.GetKeyDown(KeyCode.S))
{
if (stateInfor.normalizedTime>0.5f&&stateInfor.normalizedTime<0.8f)
{
ani.SetTrigger("Attack2");
}
//if (isCanPlaySkill)
//{
//ani.SetTrigger("Attack2");
//}
}
if (Input.GetKeyDown(KeyCode.D))
{
ani.SetTrigger("Death");
}
if (Input.GetKeyDown(KeyCode.E))
{
ani.SetTrigger("Exit");
}
//Run2
if (Input.GetKeyDown(KeyCode.Z))
{
ani.SetBool("Run2",true);
}
//idle2
if (Input.GetKeyDown(KeyCode.I))
{
ani.SetBool("Idle2",true);
}
//walk2
if (Input.GetKeyDown(KeyCode.O))
{
ani.SetBool("Walk2",true);
}
if (Input.GetKeyDown(KeyCode.Q))
{
ani.SetBool("Idle2",false);
ani.SetBool("Walk2",false);
ani.SetTrigger("QuitCurrentLayer");
}
float v=Input.GetAxis("Vertical");//-1~1
ani.SetFloat("Blend",v,1f,Time.deltaTime);
}
}
void StartSkillEvent()
{
//Debug.Log ("可以连招了");
isCanPlaySkill = true;
}
void StopSkillEvent()
{
//Debug.Log ("不可以连招了");
isCanPlaySkill = false;
}
}