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