본문 바로가기

언리얼엔진/C++슈팅프로젝트제작

충돌시 파티클효과 발생

Bullet과 충돌시 파티클효과를 발생하기 위해  public으로 파티클 포인트를 선언해 줍니다.

Bullet.h

	UPROPERTY(EditAnywhere)
	class UParticleSystem* explosionFX;

폭발 이펙트는 에너미와 충돌이 발생했을때 발생되어야 하므로 OnBulletOverlap()함수에서 에너미를 제거하는 다음 줄에 이펙트 생성코드를 추가합니다. 파티클 파일을 월드에 생성할 때는 UGameplayStatics 클래스의 SpawnEmitterAtLocation()함수를 사용합니다  접근하려면 Kismet/GamePlayStatics.h를 포함시켜줘야 합니다.

Bullet.cpp

#include "Bullet.h"
#include "Components/BoxComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/ArrowComponent.h"
#include "EnemyActor.h"
#include "Kismet/GameplayStatics.h"

UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), explosionFX, GetActorLocation(), GetActorRotation()); 추가합니다.

void ABullet::OnBulletOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, 
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
//충돌이벤트가 발생할때 실행할 함수
{
	AEnemyActor* enemy = Cast<AEnemyActor>(OtherActor);
	UE_LOG(LogTemp, Display, TEXT("OtherActor is %s"), *enemy->GetActorNameOrLabel());
	if (enemy != nullptr)
		OtherActor->Destroy();
	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), explosionFX, GetActorLocation(), GetActorRotation());
	Destroy();
}

코드를 빌드하고 BP_Bullet블루프린트 설정창에서 디테일 패널의 Explosion FX항목에 P_Explosion을 할당합니다.

총알이 에너미와 충돌하면 폭발이 일어 납니다.