unity学习41:动画里的曲线curve参数 和 事件 events
目录
1 曲线 curve
1.1 生成和修改曲线
1.2 曲线命名 = animator参数命名,关联起来
1.3 可以修改animator的参数,也可以获取animator的参数
1.4 用脚本获得曲线的参数数值,并打印出来
1.4.1 获得曲线的test1参数
1.4.2 代码
1.4.3 测试OK
1.4.4 应用
2 事件 Events
2.1 增加新事件
2.2 事件的核心是 函数 function
2.3 具体代码
2.4 报错处理
1 曲线 curve 和参数
1.1 生成和修改曲线
- 点击+号 新建曲线
- 默认有2个key帧,默认是直线
- 可以右键添加新的key
- 拖动曲线上的key点,可以调整曲线形状
可以生成多条曲线
1.2 曲线命名 = animator参数命名,关联起来
- 曲线命名= animator参数命名,关联起来
- 命名必须相同
- 修改曲线名字后,一定记得点apply 才会生效
1.3 可以修改animator的参数,也可以获取animator的参数
也可以从animator里去 设置参数
animator1.SetBool("IsRun",false);
也可以从animator里获得参数
比如 get
animator1.GetBool("IsRun");
实际上可以
- animator1.GetBool("IsRun");
- nimator1.GetFloat("test1")
- 等等
1.4 用脚本获得曲线的参数数值,并打印出来
1.4.1 获得曲线的test1参数
- //获得曲线的test1参数
- Debug.Log(animator1.GetFloat("test1"));
1.4.2 代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestPlayer1 : MonoBehaviour
{
private Animator animator1;
// Start is called before the first frame update
void Start()
{
animator1=GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float horzontal=Input.GetAxis("Horizontal");
float vetical=Input.GetAxis("Vertical");
Vector3 dir1=new Vector3(horzontal,0,vetical);
Debug.DrawRay(transform.position,dir1,Color.red);
//如果按下了移动按键
if(dir1 != Vector3.zero)
{
//面向向量
transform.rotation=Quaternion.LookRotation(dir1);
//播放跑步动画
animator1.SetBool("IsRun",true);
//朝着面向的前方移动
transform.Translate(Vector3.forward*2*Time.deltaTime);
}else
{
//播放walk动画
animator1.SetBool("IsRun",false);
}
if(Input.GetKeyDown(KeyCode.Q))
{
//触发wave参数
GetComponent<Animator>().SetTrigger("wave");
}
//获得曲线的test1参数
Debug.Log(animator1.GetFloat("test1"));
}
}
1.4.3 测试OK
- 因为我是在walk动作上挂的曲线
- 所以walk时,这个数值变化
- run起来后,这个数值就不变了
1.4.4 应用
- 可以应用于绑定在 这个动作上的一些其他行为
- 比如,攻击特效,走路效果,声音,等等
- 比如挥拳,声音逐渐变小
- 比如挥拳,特效逐渐变大,变最大,然后消失
2 事件 Events
2.1 增加新事件
- 拖动动画,寻找一些特殊帧
- 特殊帧可以对应设置事件
- 点击前面竖线+ 可以设置事件
- 比如选择 rightfoot 和 leftfoot 着地的时间,设置事件
- 这2个事件 rightfoot ,leftfoot 在Animator里就是函数
2.2 事件的核心是 函数 function
- void rightfoot() {}
- void rleftfoot() {}
- 可以在 player 这个gameObject挂的脚本上直接使用
void rightfoot()
{
Debug.Log("右脚");
}
void leftfoot()
{
Debug.Log("左脚");
}
2.3 具体代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestPlayer1 : MonoBehaviour
{
private Animator animator1;
// Start is called before the first frame update
void Start()
{
animator1=GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float horzontal=Input.GetAxis("Horizontal");
float vetical=Input.GetAxis("Vertical");
Vector3 dir1=new Vector3(horzontal,0,vetical);
Debug.DrawRay(transform.position,dir1,Color.red);
//如果按下了移动按键
if(dir1 != Vector3.zero)
{
//面向向量
transform.rotation=Quaternion.LookRotation(dir1);
//播放跑步动画
animator1.SetBool("IsRun",true);
//朝着面向的前方移动
transform.Translate(Vector3.forward*2*Time.deltaTime);
}else
{
//播放walk动画
animator1.SetBool("IsRun",false);
}
if(Input.GetKeyDown(KeyCode.Q))
{
//触发wave参数
GetComponent<Animator>().SetTrigger("wave");
}
//获得曲线的test1参数
//Debug.Log(animator1.GetFloat("test1"));
}
void rightfoot()
{
Debug.Log("右脚");
}
void leftfoot()
{
Debug.Log("左脚");
}
}
2.4 报错处理
- 如果出现如下的错误,一般是因为没有把 这2个函数放在根目录下
- void rightfoot() {}
- void rleftfoot() {}
- 也就是这2个不能位于 start() 或者 update() 之内
- 因为 start() 只开始执行1次
- update()一帧内,动画基本执行不到这