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);
        }       
    }
}

  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值