Actor 를 부모로 C++ 클래스를 Public으로 만들자

header public:부분에 콜라이더와 스태틱매시 컴포넌트 변수 선언
public:
UPROPERTY(EditAnywhere)
class UBoxComponent* boxComp;
UPROPERTY(EditAnywhere)
class UStaticMeshComponent* meshComp;
EnemyActor.cpp에서 헤드파일을 추가하고 생성자에서 박스컴포넌트와, 스태틱메시컴포넌트 오브젝트를 만들어 연결해줍니다.
#include "Components/BoxComponent.h"
AEnemyActor::AEnemyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
boxComp = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollider"));
SetRootComponent(boxComp);
boxComp->SetBoxExtent(FVector(50.f, 50.f, 50.f));
meshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
meshComp->SetupAttachment(boxComp);
}
BP_Enemy 를 열고 mesh컴포넌트를 큐브로 설정해 줍니다 사이즈는 100짜리로 선택합니다.
추첨에 의해 이동 방향 결정 기능
EnemyActor.h에 traceRate와 dir 변수를 마련합니다. traceRate는 UPROPERTY로 에디터에 노출합니다 publc: 도메인으로 해야 합니다. 이동을 위한 moveSpeed 변수도 미리 추가합니다.
public:
UPROPERTY(EditAnywhere)
int32 traceRate = 30;
UPROPERTY(EditAnywhere)
float moveSpeed = 800;
private:
FVector dir;
EnmeyActor.cpp에서 헤더파일을 추가
#include "EngineUtils.h"
#include "PlayerPawn.h"
난수가 traceRate보다 작을 경우 방향을 앞쪽이 아닌 플레이어쪽으로 바꾸고 있습니다.
플레이어를 찾기위해 APlayerPawn 객체를 다 뒤져서 이름을 검사하는데 좀더 간단한 방법을 사용했습니다.
확율보다 클경우 Forward방향을 지정합니다.
void AEnemyActor::BeginPlay()
{
Super::BeginPlay();
int32 drawResult = FMath::RandRange(1, 100);
if (drawResult <= traceRate)
{
//for (TActorIterator<APlayerPawn> player(GetWorld()); player; ++player)
//{
// if (player->GetName().Contains(TEXT("BP_PlayerPawn")))
// {
// dir = player->GetActorLocation() - GetActorLocation();
// dir.Normalize();
// }
//}
dir = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation()
- GetActorLocation();
}
else
{
dir = GetActorForwardVector();
}
}
방향을 정했으니 Tick()함수때 액터를 이동시켜줍니다.
// Called every frame
void AEnemyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector newLocation = GetActorLocation() + dir * moveSpeed * DeltaTime;
SetActorLocation(newLocation);
}
'언리얼게임프로젝트 > 슈팅게임 C++' 카테고리의 다른 글
Ureal C++ Collision처리 (0) | 2025.03.12 |
---|---|
EnemyActor Factory 생성하기 Spawn (0) | 2025.03.12 |
Unreal Shooting Game bullet 총알제작 USoundBase (0) | 2025.03.11 |
Unreal C++ 플레이어 제작하기 (0) | 2025.03.11 |
Unreal Shooting C++ 프로젝트, 게임모드, Orthographic (0) | 2025.03.11 |