博客
关于我
unity5.0的StateMachineBehaviours
阅读量:77 次
发布时间:2019-02-25

本文共 1426 字,大约阅读时间需要 4 分钟。

作用:Unity中在人物播放某个动画时往往伴随着动画音效的播放,或则是人物打击的粒子特效播放

旧方法:Animation(Ctrl+6)动画中添加Event事件,然后填写注册的方法名,Unity会在挂载该动画的物体上查找该方法,在动画播放到定义事件的那一帧时,会执行该方法。使用StateMachineBehaviours明显会方便很多

例子:在人物攻击时播放攻击特效,攻击动画结束关闭攻击特效

首先类继承StateMachineBehaviour,然后重写OnStateEnter,OnStateExit方法

override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex){   }

Animator动画参数是特定的animator ,是这个状态机行为的引用。

· AnimatorStateInfo 是状态机的行为是对state 的当前信息。相当于writing animator.GetCurrentStateInfo(layerIndex);

· LayerIndex 是状态机行为状态的layer 层。例如,0为基底图层,1用于第一个 等等。

步骤:将特效物体放在人物下成为子物体

在这里插入图片描述

AttackSMB.cs

using System.Collections;using System.Collections.Generic;using UnityEngine;public class AttackSMB : StateMachineBehaviour{       public int index;    Transform effect;    public override void OnStateEnter(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex)    {           //显示武器        //animator.transform.GetComponent
().ShowWeapon(); //获取特效 effect= animator.transform.Find("TrailEffect/Ellen_Staff_Swish0" + index); //显示特效 effect.gameObject.SetActive(false); effect.gameObject.SetActive(true); } public override void OnStateExit(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex) { //animator.transform.GetComponent
().HideWeapon(); //隐藏特效 effect.gameObject.SetActive(false); }}

最后在对应动画上添加AttackSMB脚本,修改index值即可

在这里插入图片描述

转载地址:http://uex.baihongyu.com/

你可能感兴趣的文章
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>