在游戏中,许多音效需要在动画恰当的时机出现,例如行走、奔跑,就需要恰好在足部落地瞬间播放。

而AnimNotify就能非常方便地处理此类问题。

AnimNotify,顾名思义就是动画通知,能在特定的动画片段播放到特定进度时“发出消息”。

目前我们的工程有前、后、左、右、左前、右前、左后、右后八向的跑动动画。

先以向前跑为例,用右键添加通知的方式,分别在右脚、左脚落地时添加了lfoot_touchground与rfoot_touchground的两个自定义通知

当然直接添加playsound通知也是可以的,能很方便地直接设置声音,但为了功能的可扩展性我们还是使用自定义通知。

然而我们如何才能获取这些通知呢?

if (mesh) {
UAnimInstance* anim=mesh->GetAnimInstance();
if (anim) {
TArray<const struct FAnimNotifyEvent*> AnimNotifies=anim->NotifyQueue.AnimNotifies;
range(i,,AnimNotifies.Num()) {
FString NotifyName=AnimNotifies[i]->NotifyName.ToString();
/*GEngine->A/ddOnScreenDebugMessage
(-1, 5.f, FColor::Red, FString::FromInt(i)+" "+ NotifyName);*/
if (NotifyName=="lfoot_touchground") {
audio_lfoot->SetSound(sb_walks[]);
audio_lfoot->Play();
}
else if (NotifyName == "rfoot_touchground") {
audio_rfoot->SetSound(sb_walks[]);
audio_rfoot->Play();
}
else { }
}
}
}

没错,就是这么简单anim->NotifyQueue即为动画通知队列,AnimNotifies就能获取当前所有通知事件,上述代码去掉注释即可打印当前帧所有动画通知名称。

为了实现播放音效,我们还需要绑定在左右脚的AudioComponent,如果当前通知队列中有对应的通知,就先SetSound设置将要播放的声音资源后再Play。

我把所有需要绑定在Chracter的Mesh上的AudioComponent“打包装入“了一个叫AudioController的类,在Character的构造函数中进行了AudioController的构造,并将AudioController中的各音效组件绑定到对应的socket上。

AudioController.h文件

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Components/ActorComponent.h"
#include "Components/AudioComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "AudioController.generated.h" UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYSOUNDANDEFFECTS_API UAudioController : public UActorComponent
{
GENERATED_BODY() public:
// Sets default values for this component's properties
UAudioController();
//UAudioController(USkeletalMeshComponent* mesh_); protected:
// Called when the game starts
virtual void BeginPlay() override; public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; UPROPERTY(Category = Audio, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UAudioComponent* audio_lfoot = NULL;
UPROPERTY(Category = Audio, EditDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
FString bonename_lfoot = "Bip01-L-Foot"; UPROPERTY(Category = Audio, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UAudioComponent* audio_rfoot = NULL;
UPROPERTY(Category = Audio, EditDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
FString bonename_rfoot = "Bip01-R-Foot"; USkeletalMeshComponent* mesh = NULL; void my_init(USkeletalMeshComponent* mesh_); UPROPERTY(Category = Audio, EditDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
TArray<class USoundBase*> sb_walks; };

AudioController.cpp文件

// Fill out your copyright notice in the Description page of Project Settings.

#include "MySoundAndEffects.h"
#include "Animation/AnimInstance.h"
#include "AudioController.h" // Sets default values for this component's properties
UAudioController::UAudioController()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
audio_lfoot = CreateDefaultSubobject<UAudioComponent>("audio_lfoot");
audio_rfoot = CreateDefaultSubobject<UAudioComponent>("audio_rfoot"); } //UAudioController::UAudioController(USkeletalMeshComponent* mesh_)
//{
// // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// // off to improve performance if you don't need them.
// PrimaryComponentTick.bCanEverTick = true;
//
// // ...
//
//
//
//} void UAudioController::my_init(USkeletalMeshComponent* mesh_) {
this->mesh = mesh_;
//UAudioController();
audio_lfoot->SetupAttachment(mesh_, FName(*bonename_lfoot));
audio_rfoot->SetupAttachment(mesh_, FName(*bonename_rfoot));
} // Called when the game starts
void UAudioController::BeginPlay()
{
Super::BeginPlay(); // ... } // Called every frame
void UAudioController::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); // ...
if (mesh) {
UAnimInstance* anim=mesh->GetAnimInstance();
if (anim) {
TArray<const struct FAnimNotifyEvent*> AnimNotifies=anim->NotifyQueue.AnimNotifies;
range(i,,AnimNotifies.Num()) {
FString NotifyName=AnimNotifies[i]->NotifyName.ToString();
/*GEngine->A/ddOnScreenDebugMessage
(-1, 5.f, FColor::Red, FString::FromInt(i)+" "+ NotifyName);*/
if (NotifyName=="lfoot_touchground") {
audio_lfoot->SetSound(sb_walks[]);
audio_lfoot->Play();
}
else if (NotifyName == "rfoot_touchground") {
audio_rfoot->SetSound(sb_walks[]);
audio_rfoot->Play();
}
else { }
}
}
}
else {
throw std::exception("mesh not exist!!");
} }

还有character类中audiocontroller的定义和创建:

AAimPraticeHuman::AAimPraticeHuman()
{
#ifdef AudioController_h
audiocontroller = CreateDefaultSubobject<UAudioController>("audiocontroller");
audiocontroller->my_init(GetMesh());
#endif
}

 

UPROPERTY(Category = Audio, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
  class UAudioController* audiocontroller = NULL;

最后就是音效导入,看似简单其实有很多细节。

建议直接拖入wav格式文件,如果导入失败应该是wav文件具体参数的问题。

(我用Audition把一长串的跑步音效分割出了两声脚碰地的声音,直接导入ue4报错,然而先用格式工厂转一下就ok了)

当然现在的音效还不能直接用于游戏脚步,还需要先设置并发和立体效果。

Concurrency项勾选override即可,使用”先远后旧“的并发停止策略问题也不大。

Attenuation项我新建了一个叫SceneSoundAttenuation蓝图,设置如下:

这些选项看字面都比较好理解,3D Stereo Spread的意思其实就是左右耳间距。

最后别忘了设置character蓝图的sb_walks数组的值,以及左右脚的socketname

大功告成啦!

具体效果。。。反正你们也听不到。。。

------------------------------------------------

下期预告:美妙的IK(反向动力学)

ue4音效、动画结合实例的更多相关文章

  1. Wpf(Storyboard)动画简单实例

    原文:Wpf(Storyboard)动画简单实例 动画的三种变换方式 RotateTransform:旋转变换变化值:CenterX围绕转的圆心横坐标 CenterY纵坐标 Angle旋转角度(角度正 ...

  2. jQuery动画特效实例教程

    本文以实例形式详细讲述了jQuery动画特效的实现方法. 1.自制折叠内容块 内容块如下:     <div class="module">   <div cla ...

  3. UE4帧动画Matineed

    发一句牢骚,ue4除了渲染好一点,其他操作都没有unity便利,最近需要在项目中,调几个简单的动画使用到了Matineed,相当不好用.也可能是unity转ue4,有先入为主的观念,哈哈,never ...

  4. 微信小程序之自定义模态弹窗(带动画)实例

    1.基本需求. 实现用户自定义弹框 带动画(动画可做参靠,个人要是觉得不好看可以自定义动画) 获取弹出框的内容,自定义事件获取 2.案例目录结构 二.程序实现具体步骤 1.弹框index.wxml代码 ...

  5. [UE4]蒙太奇动画运行时不播放,预览是好的

    动画实例里面没有添加“DefaultSlot”就会出现这样的问题

  6. UE4中动画蒙太奇的合成

    在游戏中的技能施法动作是可以通过软件合成的,笔者在这里介绍一种用UE4合成多个动画的操作. 在UE4中角色的动作可以由多种方式达成,一种是混合空间,例如角色的跑动和跳跃,其中的动作是由状态机控制的,原 ...

  7. [UE4]Montage动画设置Slot

    最后一张图看下,配合官网motage教程,容易理解push anim具体用法 http://aigo.iteye.com/blog/2277545 如何新建一个Montage的步骤这里省略了,网上很多 ...

  8. 关于UE4音效的一些小问题

    前言 前几天实在忍受不了StarterContent默认音效的折磨,去网上翻罗了一下初中时很着迷的游戏<Bounce-Tales>的音效,在导入音效时出了点问题,特此说明一下解决方案,希望 ...

  9. Ace 动画应用实例 ------启动欢迎界面

    仔细观察手机应用很多都用到了动画效果 那么咱们也做一个 除了位移没有使用 其他都有 布局随便放张图片 public class SplashActivity extends Activity { pr ...

随机推荐

  1. JAVA基础2----数据类型和运算符

    Java数据类型 1.基本数据类型 整数:byte/short/int/long byte:-128~127 (1个字节) short:-2^15~2^15-1 (2个字节) int(默认类型):-2 ...

  2. Python Socket 简单聊天室2

    上篇文章写了一个简单的单线程的一问一答的简单聊天室.这次我们使用SocketServer模块搭建一个多线程异步的聊天室. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  3. selenium + ChromeDriver

    Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.而对于爬虫来说,使用Selenium操控浏览器来爬取网上的数据那么肯定是爬虫中的杀 ...

  4. NYOJ116 士兵杀敌(二)

    士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...

  5. 响应式布局 —— Demo

    响应式布局实例演示What is 响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端--而不是为每个终端做一个特定的版本.这 ...

  6. MAC下安装MAMP的PHPredis扩展

    下载phpredis扩展安装包.git clone https://github.com/nicolasff/phpredis.git: 解压后,进入该目录: 依次执行以下操作完成安装: /Appli ...

  7. 计蒜客模拟赛D1T3 蒜头君的坐骑:用dfs转移dp

    题目链接:https://nanti.jisuanke.com/t/16447 题意: 蒜头君有一只坐骑,人马. 一天,蒜头君骑着他的坐骑走上了一片n*m的大荒野,一开始时,蒜头君在(1,1)点,他要 ...

  8. 开源社交系统ThinkSNS+ 0.7.3研发周报

    什么是ThinkSNS+ ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+. 亲爱的粉丝,授权客户, ...

  9. 在Android中使用枚举注解而不是枚举

    Enums often require more than twice as much memory as static constants. You should strictly avoid us ...

  10. 关于视频编辑SDK的接入说明

    一.运行环境 Android 4.1(api 16)以上: 处理器:双核 1GHz以上CPU(目前只支持ARM CPU, X86.MIPS暂不支持):推荐四核1.2GHz以上CPU 内存:1 GB以上 ...