射击和死亡倒地音效
2023年12月11日 2024年1月11日
Spawn Sound At Location / Fire & Death
说明
在代码里播放音效:
- 步枪和榴弹发射器射击音效
- 榴弹发射器空射音效
Dry fire
- 游戏角色死亡倒地音效
玩家操控的游戏角色和NPC
API导航
PlaySoundAtLocation
在给定座标播放音效
1/** 2 * Plays a sound at the given location. This is a fire and forget sound and does not travel with any actor. 3 * Replication is also not handled at this point. 4 * @param Sound - sound to play 5 * @param Location - World position to play sound at 6 * @param Rotation - World rotation to play sound at 7 * @param VolumeMultiplier - A linear scalar multiplied with the volume, in order to make the sound louder or softer. 8 * @param PitchMultiplier - A linear scalar multiplied with the pitch. 9 * @param StartTime - How far in to the sound to begin playback at 10 * @param AttenuationSettings - Override attenuation settings package to play sound with 11 * @param ConcurrencySettings - Override concurrency settings package to play sound with 12 * @param OwningActor - The actor to use as the "owner" for concurrency settings purposes. Allows PlaySound calls 13 * to do a concurrency limit per owner. 14 */ 15UFUNCTION(BlueprintCallable, Category="Audio", meta=(WorldContext="WorldContextObject", AdvancedDisplay = "3", UnsafeDuringActorConstruction = "true", Keywords = "play")) 16static void PlaySoundAtLocation(const UObject* WorldContextObject, USoundBase* Sound, FVector Location, FRotator Rotation, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, class USoundAttenuation* AttenuationSettings = nullptr, USoundConcurrency* ConcurrencySettings = nullptr, const AActor* OwningActor = nullptr, const UInitialActiveSoundParams* InitialParams = nullptr); 17 18static void PlaySoundAtLocation(const UObject* WorldContextObject, USoundBase* Sound, FVector Location, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, class USoundAttenuation* AttenuationSettings = nullptr, USoundConcurrency* ConcurrencySettings = nullptr, const UInitialActiveSoundParams* InitialParams = nullptr) 19{ 20 PlaySoundAtLocation(WorldContextObject, Sound, Location, FRotator::ZeroRotator, VolumeMultiplier, PitchMultiplier, StartTime, AttenuationSettings, ConcurrencySettings, nullptr, InitialParams); 21}
SpawnSoundAttached
创建声音组件, 关联给定音效; 将音效对象挂载到给定组件的指定Socket
USoundBase为空则返回
1/** This function allows users to create and play Audio Components attached to a specific Scene Component. 2 * Useful for spatialized and/or distance-attenuated sounds that need to follow another object in space. 3 * @param Sound - sound to play 4 * @param AttachComponent - Component to attach to. 5 * @param AttachPointName - Optional named point within the AttachComponent to play the sound at 6 * @param Location - Depending on the value of Location Type this is either a relative offset from 7 * the attach component/point or an absolute world position that will be translated to a relative offset 8 * @param Rotation - Depending on the value of Location Type this is either a relative offset from 9 * the attach component/point or an absolute world rotation that will be translated to a relative offset 10 * @param LocationType - Specifies whether Location is a relative offset or an absolute world position 11 * @param bStopWhenAttachedToDestroyed - Specifies whether the sound should stop playing when the 12 * owner of the attach to component is destroyed. 13 * @param VolumeMultiplier - A linear scalar multiplied with the volume, in order to make the sound louder or softer. 14 * @param PitchMultiplier - A linear scalar multiplied with the pitch. 15 * @param StartTime - How far in to the sound to begin playback at 16 * @param AttenuationSettings - Override attenuation settings package to play sound with 17 * @param ConcurrencySettings - Override concurrency settings package to play sound with 18 * @param bAutoDestroy - Whether the returned audio component will be automatically cleaned up when the sound finishes 19 * (by completing or stopping) or whether it can be reactivated 20 * @return An audio component to manipulate the spawned sound 21 */ 22UFUNCTION(BlueprintCallable, Category="Audio", meta=(AdvancedDisplay = "2", UnsafeDuringActorConstruction = "true", Keywords = "play")) 23static UAudioComponent* SpawnSoundAttached(USoundBase* Sound, USceneComponent* AttachToComponent, FName AttachPointName = NAME_None, FVector Location = FVector(ForceInit), FRotator Rotation = FRotator::ZeroRotator, EAttachLocation::Type LocationType = EAttachLocation::KeepRelativeOffset, bool bStopWhenAttachedToDestroyed = false, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, USoundAttenuation* AttenuationSettings = nullptr, USoundConcurrency* ConcurrencySettings = nullptr, bool bAutoDestroy = true); 24 25static class UAudioComponent* SpawnSoundAttached(USoundBase* Sound, USceneComponent* AttachToComponent, FName AttachPointName, FVector Location, EAttachLocation::Type LocationType = EAttachLocation::KeepRelativeOffset, bool bStopWhenAttachedToDestroyed = false, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, USoundAttenuation* AttenuationSettings = nullptr, USoundConcurrency* ConcurrencySettings = nullptr, bool bAutoDestroy = true) 26{ 27 return SpawnSoundAttached(Sound, AttachToComponent, AttachPointName, Location, FRotator::ZeroRotator, LocationType, bStopWhenAttachedToDestroyed, VolumeMultiplier, PitchMultiplier, StartTime, AttenuationSettings, ConcurrencySettings, bAutoDestroy); 28}
UAudioComponent
-
播放绑定音效
1/** Begins playing the targeted Audio Component's sound at the designated Start Time, seeking into a sound. 2 * @param StartTime The offset, in seconds, to begin reading the sound at 3 */ 4UFUNCTION(BlueprintCallable, Category="Audio|Components|Audio") 5virtual void Play(float StartTime = 0.0f);
-
停止播放音效
1/** Stop an audio component's sound, issue any delegates if needed */ 2UFUNCTION(BlueprintCallable, Category="Audio|Components|Audio") 3virtual void Stop();
PlaySoundAtLocation
创建声音组件, 关联给定音效; 播放之
1/** 2 * Spawns a sound at the given location. This does not travel with any actor. Replication is also not handled at this point. 3 * @param Sound - sound to play 4 * @param Location - World position to play sound at 5 * @param Rotation - World rotation to play sound at 6 * @param VolumeMultiplier - A linear scalar multiplied with the volume, in order to make the sound louder or softer. 7 * @param PitchMultiplier - A linear scalar multiplied with the pitch. 8 * @param StartTime - How far in to the sound to begin playback at 9 * @param AttenuationSettings - Override attenuation settings package to play sound with 10 * @param ConcurrencySettings - Override concurrency settings package to play sound with 11 * @param bAutoDestroy - Whether the returned audio component will be automatically cleaned up when the sound finishes 12 * (by completing or stopping) or whether it can be reactivated 13 * @return An audio component to manipulate the spawned sound 14 */ 15UFUNCTION(BlueprintCallable, Category="Audio", meta=(WorldContext="WorldContextObject", AdvancedDisplay = "3", UnsafeDuringActorConstruction = "true", Keywords = "play")) 16static UAudioComponent* SpawnSoundAtLocation(const UObject* WorldContextObject, USoundBase* Sound, FVector Location, FRotator Rotation = FRotator::ZeroRotator, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, class USoundAttenuation* AttenuationSettings = nullptr, USoundConcurrency* ConcurrencySettings = nullptr, bool bAutoDestroy = true);
创建Sound Cue资产
ExternalContent/Sounds/ | Content/Sounds/ | 分组 | 声音衰减 | |||
---|---|---|---|---|---|---|
游戏角色死亡倒地音效 | Foley/wav | sw_Foley_Bodyfall_01, 02 | SCue_CharacterDeath | Character | SC_Character | Foley |
步枪射击音效 | Weapons/AssultRifle/wav | sw_Wep_AR_Shoot_LP_01 | SCue_RifleFire; 勾选Loop | Weapon/Rifle | SC_Weapon | Wep_Shoot |
榴弹发射器射击音效 | Weapons/GrenadeLauncher/wav | sw_Wep_Launcher_Shoot_01 - 04 | SCue_LauncherFire | Weapon/Launcher | SC_Weapon | Wep_Shoot |
榴弹发射器空射音效 | Weapons/GrenadeLauncher/wav | sw_Wep_Launcher_Dryfire_01 - 03 | SCue_LauncherNoAmmo | Weapon/Launcher | SC_Weapon | Wep_Shoot |
死亡倒地音效
protected
ShootThemUp: Player/STUBaseCharacter.h
1class USoundCue; 2 3UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) 4USoundCue *DeathSound;
ShootThemUp: Player/STUBaseCharacter.cpp
1#include "Sound/SoundCue.h" 2#include "Kismet/GameplayStatics.h" 3 4// OnDeath 5UGameplayStatics::PlaySoundAtLocation(GetWorld(), DeathSound, GetActorLocation());
射击音效
基类
添加属性: 保存射击音效
protected
ShootThemUp: Weapon/STUBaseWeapon.h
1class USoundCue; 2 3UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) 4USoundCue *FireSound;
步枪
和枪口特效一样, 有组件, 在首次射击时初始化, 停止射击时暂停, 再次射击时继续
- 修改InitMuzzleFX为InitFX
- 修改SetMuzzleFXVisibility(Visible)为SetFXActive(IsActive)
- 添加声音组件, 首次射击时初始化
private
ShootThemUp: Weapon/STURifleWeapon.h
1class UAudioComponent; 2 3UPROPERTY() 4UAudioComponent *FireAudioComponent;
ShootThemUp: Weapon/STURifleWeapon.cpp
1#include "Kismet/GameplayStatics.h" 2#include "Components/AudioComponent.h" 3#include "Sound/SoundCue.h" 4 5// InitFx 6if (!FireAudioComponent) 7{ 8 FireAudioComponent = UGameplayStatics::SpawnSoundAttached(FireSound, WeaponMeshComponent, MuzzleSocketName); 9}
- 随枪口特效使能/停止
ShootThemUp: Weapon/STURifleWeapon.cpp
1void ASTURifleWeapon::SetFXActive(bool IsActive) 2{ 3 if (MuzzleFXComponent) 4 { 5 MuzzleFXComponent->SetPaused(!IsActive); 6 MuzzleFXComponent->SetVisibility(IsActive, true); 7 } 8 9 if (FireAudioComponent) 10 { 11 IsActive ? FireAudioComponent->Play() : FireAudioComponent->Stop(); 12 } 13}
榴弹发射器
ShootThemUp: Weapon/STULauncherWeapon.cpp
1#include "Kismet/GameplayStatics.h" 2#include "Sound/SoundCue.h" 3#include "Components/SkeletalMeshComponent.h" 4 5// MakeShot最后 6UGameplayStatics::SpawnSoundAttached(FireSound, WeaponMeshComponent, MuzzleSocketName);
榴弹发射器空射音效
ShootThemUp: Weapon/STULauncherWeapon.h
1class USoundCue; 2 3UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) 4USoundCue *NoAmmoSound;
ShootThemUp: Weapon/STULauncherWeapon.cpp
1// MakeShot 2 3// if (IsAmmoEmpty() || !GetWorld()) return; 4 5if (IsAmmoEmpty()) 6{ 7 UGameplayStatics::SpawnSoundAtLocation(GetWorld(), NoAmmoSound, GetActorLocation()); 8 return; 9} 10 11if (!GetWorld()) return;
设置Sound Cue资产
配置项 | 值 | |
---|---|---|
BP_STUPlayerCharacter | Death Sound | SCue_CharacterDeath |
BP_STUAICharacter | Death Sound | SCue_CharacterDeath |
BP_STURifleWeapon | Fire Sound | SCue_RifleFire |
BP_STULauncherWeapon | No Ammo Sound | SCue_LauncherNoAmmo |
BP_STULauncherWeapon | Fire Sound | SCue_LauncherFire |
查看
- 武器射击声音很大, 衰减梯度不明显
- 倒地声音需要离得很近才能听到
测试时停止NPC行为树, 击杀NPC - 步枪点射时会出现没有声音的情况