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

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


http://www.kler.cn/a/549122.html

相关文章:

  • ElementUI 的组件 Switch(开关)如何让文字显示在按钮上
  • Rust编程语言入门教程(一)安装Rust
  • 【云安全】云原生- K8S Kubelet 未授权访问
  • HTTP 与 HTTPS:协议详解与对比
  • Qt5开发入门指南:从零开始掌握跨平台开发
  • 图论(四):图的中心性——度中心性介数中心性紧密中心性
  • Redis 03章——10大数据类型概述
  • Flutter Gradle 命令式插件正式移除,你迁移旧版 Gradle 配置了吗?
  • 基于deepseek api和openweather 天气API实现Function Calling技术讲解
  • Kafka日志数据深度解析:从基础查看到高级操作全攻略
  • Testin云测(兼容性测试)
  • WeMos D1+PIR+Android 的小场景制作
  • Ubuntu 22.04 Desktop企业级基础配置操作指南
  • 「软件设计模式」适配器模式(Adapter)
  • 前端面试手写--虚拟列表
  • QT基础一、学会建一个项目
  • 基于单片机控制的电动汽车双闭环调速系统(论文+源码)
  • 【C++】vector的使用练习 + 模拟实现
  • 解决前后端日期传输因时区差异导致日期少一天的问题
  • 当时只道是寻常