关于Unity中使用MatchTarget完成人物动画匹配度,MatchTarget的简单使用
在角色进行翻墙动作时,能明显发现角色的左手是没有触碰到障碍物的,所以我们希望角色的左手能够和障碍物能有良好的接触,即手掌能贴合在障碍物的上方
跳跃动画的变量名以及条件设置
初始化
public class PlayerControl : MonoBehaviour
{
private Animator anim;
private int IsVaultID = Animator.StringToHash("IsVault");
private Vector3 MatchTaget = Vector3.zero;
void Start()
{
anim = GetComponent<Animator>();
}
使用MatchTarget函数
private void ProcessVault()
{
bool IsVault = false;//设置控制跳跃动画播放的变量
//进行射线检测,从主角身前发射射线
RaycastHit Hit;
if (Physics.Raycast(transform.position + Vector3.up * 0.3f, transform.forward, out Hit, 3f))
{
if (Hit.collider.tag == "Obstacle")//检测碰撞体标签
{
Vector3 point = Hit.point;
point.y = Hit.collider.transform.position.y + Hit.collider.bounds.size.y;//将碰撞点的高度进行设置
MatchTaget = point;
if (Input.GetKeyDown(KeyCode.Space))
{
IsVault = true;
}
if (Input.GetKeyUp(KeyCode.Space))
{
MatchTaget = Vector3.zero;//跳跃结束后将目标位置归零还原
IsVault = false;
// anim.SetBool(IsVaultID, IsVault);
}
}
}
anim.SetBool(IsVaultID, IsVault);
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Vault") && anim.IsInTransition(0) == false)//判断玩家现在是否处于跳跃阶段,动画融合是否为false
{
anim.MatchTarget(MatchTaget, Quaternion.identity, AvatarTarget.LeftHand, new MatchTargetWeightMask(Vector3.one, 0), 0.07f, 0.16f);//匹配目标位置;旋转;选择某个部位匹配、匹配目标权重;用于匹配的时间段
}
}
容易理解出错的地方
point.y = Hit.collider.transform.position.y + Hit.collider.bounds.size.y;
Hit.collider.transform.position.y不是射线检测触碰到的点,而是碰撞体的坐标,所以需要加上碰撞体模型的高度,在实际情况可以进行微调。
anim.MatchTarget(MatchTaget, Quaternion.identity, AvatarTarget.LeftHand, new MatchTargetWeightMask(Vector3.one, 0), 0.07f, 0.16f);
最后的0.07f到0.16f是在整个动画中用于匹配的时间段,选择快到需要匹配的时间时作为开始时间,进行匹配的时候作为结束时间,然后进行调整