在游戲中,許多音效需要在動畫恰當的時機出現,例如行走、奔跑,就需要恰好在足部落地瞬間播放。
而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,0,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[0]); audio_lfoot->Play(); } else if (NotifyName == "rfoot_touchground") { audio_rfoot->SetSound(sb_walks[1]); 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,0,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[0]); audio_lfoot->Play(); } else if (NotifyName == "rfoot_touchground") { audio_rfoot->SetSound(sb_walks[1]); 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(反向動力學)