文中使用代码出自:点击打开链接
增加动画切换控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ApplyRootMotion : MonoBehaviour
{
//-- Root Motion 控制变量
public Transform m_rootBone;
Vector3 m_lastRootPos;
Vector3 m_rootMotion;
int m_lastAnimTime;
public Animator anim;
public SkinnedMeshRenderer skinMesh;
void Start()
{
anim = this.GetComponent<Animator>();
//-- 从SkinnedMeshRenderer中读取Root Bone
//m_rootBone = skinMesh.rootBone;
//-- 变量初始化
m_rootMotion = Vector3.zero;
m_lastRootPos = m_rootBone.localPosition;
m_lastAnimTime = 0;
}
void LateUpdate()
{
AnimatorStateInfo animState = anim.GetCurrentAnimatorStateInfo(0);
if (animState.IsName("Take 001")|| animState.IsName("Take 001 1") || animState.IsName("Take 001 2") || animState.IsName("Take 001 3"))
{
if ((int)animState.normalizedTime > m_lastAnimTime)
{
//-- 动画循环处理
m_lastRootPos = m_rootBone.localPosition;
m_rootMotion = Vector3.zero;
}
else
{
//-- 计算当前帧的Root Motion
Vector3 rootPos = m_rootBone.localPosition;
m_rootMotion = rootPos - m_lastRootPos;
m_lastRootPos = rootPos;
rootPos.x = 0;
rootPos.z = 0;
m_rootMotion.y = 0;
m_rootBone.localPosition = rootPos;
}
m_lastAnimTime = (int)animState.normalizedTime;
}
else
{
m_lastRootPos = m_rootBone.localPosition;
m_rootMotion = Vector3.zero;
}
}
void Update()
{
if (!isMove) return;
//-- Apply Root Motion
Vector3 nextPos = this.transform.position + m_rootMotion;
this.transform.position = nextPos;
}
public bool isMove = true;
public void StartMove()
{
isMove = true;
}
public void StopMove()
{
isMove = false;
}
}
//通过程序在播放动画的时候增加人物位移,但是这种方式会有滑步。
public class Move : MonoBehaviour {
public Animator ani;
public float speed = 2;
public bool isMove = false;
void Update()
{
//attack move
if (ani != null && isMove)
{
AnimatorStateInfo info = ani.GetCurrentAnimatorStateInfo(0);
if (info.IsName("Attack"))
{
Vector3 temp = this.transform.forward;
temp = -this.transform.forward * Time.deltaTime * speed;
this.transform.position += temp;
}
}
}
}
补充:
1、带位移动作 输出都是从0点开始移动信息
2、攻击之后接IDLE还原蒙皮位置信息