Unity-动画目标匹配

动画目标匹配主要指的是

当游戏中角色要以某种动作移动,该动作播放完毕后,人物的手或者脚必须落在某一个地方

比如:角色需要跳过踏脚石或者跳跃并抓住房梁

那么这时我们就需要动作目标匹配来达到想要的效果

如何实现动画目标匹配

Unity 中的 Animator 提供了对应的函数来完成该功能

使用步骤是

  1. 找到动作关键点位置信息(比如起跳点,落地点,简单理解就是真正可能产生位移的动画表现部分)

  2. 将关键信息传入 MatchTargetAPI 中

由于动画从待机切换到跳跃有过度效果,而MatchTargetAPI需要在jump动画真正开始时执行才没有问题

那我们可以给jump动画添加一个事件,在事件中调用MatchTargetAPI就没有问题

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lesson17 : MonoBehaviour
{
    public Transform targetPos;
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("jump");

        }
    }
    public void MatchTarget()
    {
        //参数一:目标位置
        //参数二:目标角度
        //参数三:匹配的骨骼位置
        //参数四:位置角度权重
        //参数五:开始位移动作的百分比
        //参数六:结束位移动作的百分比
        animator.MatchTarget(targetPos.position, targetPos.rotation, AvatarTarget.LeftFoot
                             , new MatchTargetWeightMask(Vector3.one, 1), 0.2f, 0.64f);

    }
}

注意

调用匹配动画的时机有一些限制

  1. 必须保证动画已经切换到了目标动画上

  2. 必须保证调用时动画并不是处于过度阶段而真正在播放目标动画

    如果发现匹配不正确,往往都是这两个原因造成的

  3. 需要开启 Apply Root Motion

相关推荐
椒颜皮皮虾1 天前
TensorRtSharp:在 C# 世界中释放 GPU 推理的极致性能
c#·tensorrt
行止951 天前
WinForms 彻底隐藏 滚动条的终极解决方案
c#
时光追逐者1 天前
TIOBE 公布 C# 是 2025 年度编程语言
开发语言·c#·.net·.net core·tiobe
weixin_424294671 天前
在Unity中,摄像机移动时出现“残影”或“闪烁”是常见问题,主要原因和处理方法。
unity·游戏引擎
孟无岐1 天前
【Laya】Browser 使用说明
typescript·游戏引擎·游戏程序·laya
观无1 天前
固高运动控制卡(GST800)基础应用分享
c#
天人合一peng1 天前
unity 3d 通过游戏对象的名子查到其对象
游戏·unity·游戏引擎
UnderTurrets1 天前
A_Survey_on_3D_object_Affordance
pytorch·深度学习·计算机视觉·3d
flysh051 天前
如何利用 C# 内置的 Action 和 Func 委托
开发语言·c#
纯属个人爱好1 天前
Unity2020+PicoNeo3Pro开发
unity·vr