Implementation for actor MyActor. Don’t forget to replace YOUR_API with own.
Header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class YOUR_API AMyActor : public AActor
{
GENERATED_BODY()
public:
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
UPROPERTY()
class UAudioComponent* ShootingAudioComponent;
UPROPERTY()
class UAudioComponent* ImpactAudioComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<class UNiagaraSystem> NiagaraShootingStar;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UNiagaraComponent* ShootingStarNiagaraSystem;
};
cpp
#include "MyActor.h"
#include "Components/AudioComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraSystem.h"
#include "NiagaraComponent.h"
AMyActor::AMyActor()
{
PrimaryActorTick.bCanEverTick = true;
ShootingAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Shooting Audio"));
SetRootComponent(ShootingAudioComponent);
ImpactAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Impact Audio"));
ImpactAudioComponent->SetupAttachment(ShootingAudioComponent);
ShootingStarNiagaraSystem = CreateDefaultSubobject<UNiagaraComponent>(TEXT("UNiagara Component Shooting Star"));
ShootingStarNiagaraSystem->SetupAttachment(ShootingAudioComponent);
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
FVector Location = GetActorLocation();
FRotator Rotation = GetActorRotation();
ShootingStarNiagaraSystem = UNiagaraFunctionLibrary::SpawnSystemAttached(NiagaraShootingStar,
GetRootComponent(), "", Location, Rotation, EAttachLocation::KeepRelativeOffset, true, true,
ENCPoolMethod::AutoRelease, true);
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}