0
\$\begingroup\$

I’m developing the Pong game to learn Unreal Engine 4.26 using C++.

I use this code in the class’ constructor to load the sound:

static ConstructorHelpers::FObjectFinder<USoundWave> HitSoundAsset(TEXT("/Game/Effects/pong-paddle.pong-paddle"));
    if (HitSoundAsset.Succeeded())
        HitSound = HitSoundAsset.Object;

And in OnHit event this code:

void APaddle::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
    {
        if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Paddle has been hit by: %s"), *OtherActor->GetName()));

        if (ABall* Ball = Cast<ABall>(OtherActor))
        {
            PlayHitSound();
        }
    }
}

And finally:

void APaddle::PlayHitSound()
{
    if (HitSound != nullptr)
    {
        UGameplayStatics::PlaySoundAtLocation(this, HitSound, GetActorLocation());
    }
}

But it sounds after the ball has bounced.

The sound starts at 0 and it hasn’t any silent.

enter image description here

Is there any way to play it immediately?

\$\endgroup\$
2
  • \$\begingroup\$ That code looks correct. Does the UE_LOG display immediately or is it also delayed? \$\endgroup\$
    – Stephen
    Commented May 19, 2021 at 9:35
  • \$\begingroup\$ @Stephen Thanks for you comment. I don't know what you mean with UE_LOG but the message from AddOnScreenDebugMessage display immediately. \$\endgroup\$
    – VansFannel
    Commented May 19, 2021 at 10:14

0

You must log in to answer this question.

Browse other questions tagged .