Unreal Engine - Sound play & Effect spawn

2025. 3. 5. 23:03·Unreal Engine

1. Sound Play


사운드는 .wav 파일이나 .mp3 파일을 SoundBase로 가져와서 play할 수 있다.

 

  • 아래와 같이 변수를 선언해 주고
//for weapon sound
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound")
TArray<TObjectPtr<USoundBase>> ShootSound;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound")
TArray<TObjectPtr<USoundBase>> ReloadSound;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sound")
TObjectPtr<USoundBase> NoAmmoSound;
  • 생성자에서 아래와 같이 ConstructorHelpers로 가져와서 사용하면 된다.
//sound
static ConstructorHelpers::FObjectFinder<USoundBase>ShootAsset1(TEXT("/Game/HJ/Assets/Sound/Smith_Wesson_OneShot_Close_2.Smith_Wesson_OneShot_Close_2"));
if (ShootAsset1.Succeeded())
{
    ShootSound.Add(ShootAsset1.Object);
}
static ConstructorHelpers::FObjectFinder<USoundBase>ShootAsset2(TEXT("/Game/HJ/Assets/Sound/Smith_Wesson_OneShot_Close_1.Smith_Wesson_OneShot_Close_1"));
if (ShootAsset2.Succeeded())
{
    ShootSound.Add(ShootAsset2.Object);
}

static ConstructorHelpers::FObjectFinder<USoundBase>ReloadAsset(TEXT("/Game/HJ/Assets/Sound/reload.reload"));
if (ReloadAsset.Succeeded())
{
    ReloadSound.Add(ReloadAsset.Object);
}
  • 사용하고 싶은 곳에서 UGameplayStatics::PlaySoundAtLocation 를 통해서 play 해줄 수 있다.
  • 파라메터는 아래와 같다.

//shoot sound
if (ShootSound.Num() > 0)
{
    int RandIdx = FMath::RandRange(0, ShootSound.Num() - 1);
    UGameplayStatics::PlaySoundAtLocation(GetWorld(), ShootSound[RandIdx], MuzzleLocation);
}

 

추가 - 발소리 구현하기

 

원래 발소리는 애니메이션에서 애님 노티파이를 사용해서 주로 구현하게 된다.

그러나 현재 사용하는 애니메이션이 모션 매칭을 사용해서 구현하였기 때문에 이 방식을 통해서 구현할 수 없었다.

 

대신 사용하게 된 방식은 라인 트레이스 방식이다.

  • 캐릭터의 위치에서 아래로 라인 트레이스를 쏴주고 땅에 충돌이 일어나는 순간 sound를 play하도록 해 주었다.
    • 이때, 너무 빠르게 재생되는 것을 막기 위해서 GetTimeSeconds를 통해서 제어를 해 주었다.
    • 추가적으로, 발자국 사운드를 2개를 번갈아서 재생하도록 하였다.
void APlayerCharacter::CheckFootStep()
{	
	if (GetWorld()->GetTimeSeconds() - LastFootStepTime < FootStepCoolDown)
	{
		return;
	}

	if (bIsCrouch || bIsRolling)
	{
		return;
	}
	
	FVector Start = GetActorLocation();
	FVector End = Start - FVector(0.f, 0.f, 100.f);

	FHitResult Hit;
	FCollisionQueryParams Params;
	Params.AddIgnoredActor(this);
	FooStepIdx = 0;

	if (GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, Params))
	{
		UGameplayStatics::PlaySoundAtLocation(this, FootStepSound[FooStepIdx], Hit.Location);
		FooStepIdx++;
		FooStepIdx %= 2;
	}

	LastFootStepTime = GetWorld()->GetTimeSeconds();
}

 

결과 영상

 

 

2. Effect Spawn


Effect (나이아가라) 는 UNiagaraFunctionLibrary::SpawnSystemAtLocation 함수를 통해 spawn 할 수 있다.

 

  • 나이아가라 이팩트를 만약 종료되지 않게 계속해서 유지하려면 나이아가라 컴포넌트로 생성하는 것이 좋고 아래 글에 정리된 방식으로 사용하는 것이 좋은 방식인것 같다.

https://gbleem.tistory.com/61

 

Unreal Engine - Simple Niagara (with cpp)

캐릭터가 움직이면, 해당 캐릭터의 뒷부분에 Trail 효과를 주고 싶어서 관련 기술을 찾아보던 도중 나이아가라 시스템을 알게 되었고 간단히 사용해 본 내용을 정리해 볼 것이다.1. 나이아가라 시

gbleem.tistory.com

 

이번에 정리해 볼 방식은 한순간 spawn 하고 사라지도록 하는 방식이다.

  • 아래와 같이 변수를 선언해주고 
//for effect
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player|Components")
TObjectPtr<UNiagaraSystem> BloodNiagara;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player|Components")
TObjectPtr<UNiagaraSystem> MuzzleNiagara;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player|Components")
TObjectPtr<UNiagaraSystem> BulletNiagara;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player|Components")
TObjectPtr<UNiagaraSystem> LandNiagara;
  • 생성자에서 아래와 같이 가져오면 된다. (#include "NiagaraFunctionLibrary.h" 필요)
//Niagara Assets
static ConstructorHelpers::FObjectFinder<UNiagaraSystem>BloodAsset(TEXT("/Game/SH/NS_Splash.NS_Splash")); 
BloodNiagara = BloodAsset.Object;

static ConstructorHelpers::FObjectFinder<UNiagaraSystem>MuzzleAsset(TEXT("/Game/SH/NS_MuzzleFlash.NS_MuzzleFlash"));
MuzzleNiagara = MuzzleAsset.Object;

static ConstructorHelpers::FObjectFinder<UNiagaraSystem>BulletAsset(TEXT("/Game/HJ/Material/NS_HJBullet2.NS_HJBullet2"));
BulletNiagara = BulletAsset.Object;

static ConstructorHelpers::FObjectFinder<UNiagaraSystem> LandNiagaraAsset(TEXT("/Game/HJ/Assets/NS_Steam1.NS_Steam1"));
LandNiagara = LandNiagaraAsset.Object;
  • 이후 아래와 같이 사용하면 이팩트를 생성할 수 있다.
UNiagaraFunctionLibrary::SpawnSystemAtLocation(GetWorld(), MuzzleNiagara, MuzzleLocation, GetActorRotation());


주의)

  • 위 함수를 통해 spawn 하면 자동으로 사라지게 할 수 있다.
  • 그 이유는 파라메터에 bAutoDestroy가 true로 설정되어있기 때문이다.

  • 추가적으로 설정을 해야할 점은 아래의 사진처럼 나이아가라의 Emitter state에서 loop behavior를 once로 설정하면 된다.

 

완성 모습

 

'Unreal Engine' 카테고리의 다른 글

Unreal Engine - Main Menu (흐름 위주의 정리)  (0) 2025.03.07
UE5 Issues : ADS (Aim Offset 1D)  (0) 2025.03.05
Unreal Engine - 애님 몽타주  (0) 2025.03.05
Unreal Engine - 캐릭터 앉기 및 구르기  (0) 2025.03.05
Unreal Engine - TSubclassOf & StaticClass  (0) 2025.02.27
'Unreal Engine' 카테고리의 다른 글
  • Unreal Engine - Main Menu (흐름 위주의 정리)
  • UE5 Issues : ADS (Aim Offset 1D)
  • Unreal Engine - 애님 몽타주
  • Unreal Engine - 캐릭터 앉기 및 구르기
gbleem
gbleem
gbleem 님의 블로그 입니다.
  • gbleem
    gbleem 님의 블로그
    gbleem
  • 전체
    오늘
    어제
    • 분류 전체보기 (176)
      • Unreal Engine (66)
      • C++ (19)
      • 알고리즘(코딩테스트) (26)
      • TIL (60)
      • CS (4)
      • 툴 (1)
  • 블로그 메뉴

    • 홈
    • 카테고리
  • 링크

    • 과제용 깃허브
    • 깃허브
    • velog
  • 공지사항

  • 인기 글

  • 태그

    addonscreendebugmessage
    템플릿
    blend pose
    character animation
    motion matching
    매크로 지정자
    상속
    gamestate
    actor 클래스
    DP
    map을 vector로 복사
    additive animation
    const
    싱글턴
    BFS
    cin함수
    applydamage
    enhanced input system
    Vector
    C++
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
gbleem
Unreal Engine - Sound play & Effect spawn
상단으로

티스토리툴바