当前位置: 首页 > news >正文

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()一帧内,动画基本执行不到这

相关文章:

  • CAS单点登录(第7版)17.账户注册
  • 深度学习框架探秘|TensorFlow:AI 世界的万能钥匙
  • 安科瑞光伏发电防逆流解决方案——守护电网安全,提升能源效率
  • 算法随笔_51: 表现良好的最长时间段_方法2
  • Java三大特性
  • Uniapp 短视频去水印解析工具开发实现
  • Ubuntu添加桌面快捷方式
  • 2025有哪些关键词优化工具好用
  • XML Schema anyAttribute 元素详解
  • 算法12-贪心算法
  • 解析浏览器中JavaScript与Native交互原理:以WebGPU为例
  • 应用层优秀的共享民宿物联网框架该怎么选?
  • Spring篇--AOP
  • 前端可以不用依赖后端实现导出大数据了
  • C#学习之数据转换
  • python defaultdict用法
  • ios中常见的设计原则和设计模式
  • Hadoop集群安装与配置指南(CentOS 7)
  • 力扣LeetCode: 1742 盒子中小球的最大数量
  • 5-CDE说明
  • 马克思主义理论研究教学名师系列访谈|曾瑞明:想通了才可能认准,认准了才能做好
  • 百济首次实现季度营业利润扭亏,泽布替尼销售额近57亿元
  • 两国战机均未侵入对方领空,巴方公布对印回击细节
  • 央行将增加3000亿元科技创新和技术改造再贷款额度
  • 潘功胜:央行将创设科技创新债券风险分担工具
  • 上海市政府党组会议传达学习习近平总书记重要讲话精神,部署抓好学习贯彻落实