Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

codingfarm

펀치 - 소리 재생 본문

Unreal 4/기타

펀치 - 소리 재생

scarecrow1992 2020. 7. 20. 23:12

캐릭터가 주먹을 휘두르고 타격하는 소리가 나도록 해보자

1
2
3
4
5
6
7
8
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Audio, meta = (AllowPrivateAccess = "true"))
    class USoundCue* PunchSoundCue;
 
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Audio, meta = (AllowPrivateAccess = "true"))
    class USoundCue* PunchThrowSoundCue;
 
UAudioComponent* PunchThrowAudioComponent;
UAudioComponent* PunchAudioComponent;
cs

사운드큐 포인터와 오디오컴포넌트를 헤더파일 내에 선언한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
APunchKick06Character::APunchKick06Character() {
    static ConstructorHelpers::FObjectFinder<USoundCue> PunchSoundCueObject(TEXT("SoundCue'/Game/TUTORIAL_RESOURCES/Audio/PunchSoundCue.PunchSoundCue'"));
    if (PunchSoundCueObject.Succeeded())     {
        PunchSoundCue = PunchSoundCueObject.Object;
 
        PunchAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("PunchAudioComponent"));
        PunchAudioComponent->SetupAttachment(RootComponent);
    }
 
    // load the punch throw sound cue object
    static ConstructorHelpers::FObjectFinder<USoundCue> PunchThrowSoundCueObject(TEXT("SoundCue'/Game/TUTORIAL_RESOURCES/Audio/PunchThrowSoundCue.PunchThrowSoundCue'"));
    if (PunchThrowSoundCueObject.Succeeded())     {
        PunchThrowSoundCue = PunchThrowSoundCueObject.Object;
 
        PunchThrowAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("PunchThrowAudioComponent"));
        PunchThrowAudioComponent->SetupAttachment(RootComponent);
    }
}
 
void APunchKick06Character::BeginPlay() {
    if (PunchSoundCue && PunchAudioComponent) {
        // attach the sound cue to our audio component 
        // NOTE: do not do this in the constructor as it will play the sound when the player spawns
        PunchAudioComponent->SetSound(PunchSoundCue);
    }
 
    // make sure our audio variables are initialized
    if (PunchThrowSoundCue && PunchThrowAudioComponent) {
        // attach the sound cue to our audio component 
        // NOTE: do not do this in the constructor as it will play the sound when the player spawns
        PunchThrowAudioComponent->SetSound(PunchThrowSoundCue);
    }
}
cs

생성자 내에서 해당 포인트가 참조할 레퍼런스의 주소를 설정한다

그리고 사운드컴포넌트를 만든 후에 RootComponent에 부착시킨다.

하지만 이시점에서 사운드 컴포넌트는 자신들이 플레이할 사운드큐의 정보를 지니지 않는다.

그러므로 BeginPlay에서 각 컴포넌트에 사운드큐를 부착시켜준다.

 

이제 주먹을 날릴때 PunchThrowAudio가 재생되도록 해보자.

하지만 주먹 애니메이션의 시작과 실제 주먹을 휘두르는 애니메이션 사이에는 시간차가 존재하기에

이를 고려하여 Notify기능으로 사운드 재생을 일으키도록 한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Fill out your copyright notice in the Description page of Project Settings.
 
#include "PunchThrowAnimNotify.h"
#include "PunchKick06Character.h"
#include "Engine.h"
 
void UPunchThrowAnimNotify::Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation)
{
    //GEngine->AddOnScreenDebugMessage(-1, 4.5f, FColor::Purple, __FUNCTION__);
 
    if (MeshComp != NULL && MeshComp->GetOwner() != NULL)     {
        APunchKick06Character* Player = Cast<APunchKick06Character>(MeshComp->GetOwner());
        if (Player != NULL && !Player->PunchThrowAudioComponent->IsPlaying()) {
            Player->PunchThrowAudioComponent->Play(0.f);
        }
    }
}
cs

각 애니메이션 마다 실질적으로 팔을 휘두르기 시작하는 지점이 다르기에

PunchThrowAnimNotify를 따로 심어주었다.

 

팔이 enemy에 닿았을때 PunchAudio를 재생해보자

먼저 주의사항을 알아야한다 프로젝트를 실행하여 Enemy를 타격해보면 아래 사진처럼 주먹을 한번만 휘둘러도OnAttackHit가 연달아 몇번씩 발생하는것을 볼 수 있다.

만약 OnAttackHit가 발생할때마다 사운드를 재생하면 사운드가 계속해서 처음부터 재생되면서

마지막 타격을 기준으로 정상적인재생이 일어나기에 

아래처럼 OnAttackHit내에 코드를 작성한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void APunchKick06Character::OnAttackHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    Log(ELogLevel::WARNING, __FUNCTION__);
    Log(ELogLevel::WARNING, Hit.GetActor()->GetName());
 
    // check to make sure the audio component is initialized and we are not already playing a sound
    if (PunchAudioComponent && !PunchAudioComponent->IsPlaying())
    {
        // activate the sound if it has not been already
        if (!PunchAudioComponent->IsActive()) 
        {
            PunchAudioComponent->Activate(true);
        }
        // default pitch value 1.0f
        // modify the pitch to create variance by grabbing a random float between 1.0 and 1.3
        PunchAudioComponent->SetPitchMultiplier(FMath::RandRange(1.0f, 1.3f));
        // play the sound
        PunchAudioComponent->Play(0.f);
    }
}
cs

if문을 통해 첫번째 타격 이후 추가적인 재생을 막도록 하였음에 주목하라

그리고 pitchMultiplier 기능을 통해 다양한 크기의 사운드가 들리게끔 하였다.

'Unreal 4 > 기타' 카테고리의 다른 글

flight  (0) 2020.08.08
펀치 - 발차기  (0) 2020.07.22
펀치 - 충돌 이벤트(Hit Event)  (0) 2020.07.20
펀치 - 스켈레톤 소켓  (0) 2020.07.19
펀치 - 애니메이션 몽타주  (0) 2020.07.18
Comments