欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > Unity动画系统(3)---融合树

Unity动画系统(3)---融合树

2025/9/15 14:08:15 来源:https://blog.csdn.net/renwen1579/article/details/140146925  浏览:    关键词:Unity动画系统(3)---融合树

6.1 动画系统基础2-6_哔哩哔哩_bilibili

Animator类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EthanController : MonoBehaviour
{
    private Animator ani;
    private void Awake()
    {
        ani = GetComponent<Animator>();
    }

    private void Update()
    {
        动画参数名称可以转换为一个ID【int】
        //int id = Animator.StringToHash("CanMove");
        //Debug.Log("ID"+id);
        设置后读取动画参数
        //ani.SetBool("CanMove",true);
        [使用HashID进行参数的设置或读取,效率更高]
        //ani.SetBool(id, true);

        //ani.SetFloat
        //ani.GetFloat
        //ani.SetInteger
        //ani.GetInteger
        //ani.SetTrigger
        //ani.SetTrigger [触发参数]

        ani.SetFloat("ShoutPlaySpeed", 2);
        ani.SetFloat("ShoutPlaySpeed",2,0.5f,Time.deltaTime);

        //按下方向键左键
        if (Input.GetKeyDown(KeyCode.LeftArrow)) {
            ani.SetBool("CanMove", true);
        }

        if (Input.GetKeyDown(KeyCode.RightArrow)) {
            ani.SetBool("CanMove", false);
        }

        if (Input.GetKeyDown(KeyCode.Space)) {
            ani.SetTrigger("Shout");
        }
        if (Input.GetKey(KeyCode.S)) {
            //设置喊叫动画的播放速度,有0.5s过渡时间
            ani.SetFloat("ShoutPlaySpeed",2,0.5f,Time.deltaTime);
        }
        //获取当前动画状态信息
        AnimatorStateInfo info = ani.GetCurrentAnimatorStateInfo(0);

        Debug.Log(info.IsName("Idle"));

        Debug.Log("shortNameHash:"+info.shortNameHash);

        Debug.Log("Idle:" + Animator.StringToHash("Idle"));

        //判断当前播放的动画状态是不是Idle
        //if(info.IsName("Idle"))【通过名字判断】
        //if(info.IsTag("Idle"))【通过标签判断】

        if (info.shortNameHash == Animator.StringToHash("Idle")) {
            //【通过hashid判断】【高效】
        }
    }
}

p320-p328未学习

p329(5:35)

融合树参数类型必须是float

时间缩放播放速度

根据动画参数计算阈值

方向不同时使用2D融合类型

一般使用speed and angular speed

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RobotAnimationController : MonoBehaviour
{
    [Header("平滑过渡时间")]
    [Range(0,3)]
    public float dampTime = 3f;


    [Header("移动速度")]
    public float speed = 3f;

    [Header("转身速度")]
    public float turnSpeed = 10f;
    //虚拟轴
    private float hor, ver;
    //动画组件
    private Animator ani;

    private void Awake()
    {
        ani = GetComponent<Animator>();
    }
    private void Update()
    {
        hor = Input.GetAxis("Horizontal");
        ver = Input.GetAxis("Vertical");

        //只要按下一个方向键,就走
        if (hor != 0 || ver != 0)
        {
            //设置Speed,角色动起来
            ani.SetFloat("Speed", 5.6f, dampTime, Time.deltaTime);
            //将向量转换成四元数
            Quaternion targetQua = Quaternion.LookRotation(new Vector3(hor, 0, ver));

            //lerp过去
            transform.rotation = Quaternion.Lerp(transform.rotation, targetQua, Time.deltaTime * turnSpeed);
        }
        else {
            //停下来
            ani.SetFloat("Speed", 0f);
        }       
    }
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词