본문 바로가기

카테고리 없음

C++ Bullet 클래스 생성하기

void ABullet::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FVector newLocation = GetActorLocation() + GetActorForwardVector() * moveSpeed * DeltaTime;
	SetActorLocation(newLocation);
}

총알은 키입력을 받을 필요가 없으니 Actor클래스로 만듭니다. 이름은 Bullet으로 합니다.

충돌체와 모양이 필요하므로 C++클래스를 만든후 playerPawn과 마찬가지고 UBoxComponent와 UStaticMeshComponent를 추가해줍니다.

Bullet.h

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditAnywhere)
	class UBoxComponent* boxComp;

	UPROPERTY(EditAnywhere)
	class UStaticMeshComponent* meshComp;

Bullet.cpp

#include "Bullet.h"
#include "Components/BoxComponent.h"
#include "Components/StaticMeshComponent.h"

ABullet::ABullet()
{
 	// 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("box Collider"));
	SetRootComponent(boxComp);
	boxComp->SetBoxExtent(FVector(50, 50, 50));
	boxComp->SetWorldScale3D(FVector(0.75f, 0.25f, 1.0f));
	meshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
	meshComp->SetupAttachment(boxComp);
}

boxComp->SetWorldScale3D()를 이용해 총알의 사이즈를 조정했습니다.

 

언리얼에디터를 끄고 빌드후 언리얼이 로딩되면 bullet c++클래스를 이용해 BP_Bullet 블루프린트를 만듭니다. 메시와 메터리얼은 Cube로 적당해 넣어줍니다. 

이제 총알을 움직여 보겠습니다.

Bullet.h에 public: float moveSpeed를 추가합니다.

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UPROPERTY(EditAnywhere)
	float moveSpeed = 800.f;

Bullet.cpp Tick()함수에 이동하는 코드를 추가합니다.

void ABullet::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FVector newLocation = GetActorLocation() + GetActorForwardVector() * moveSpeed * DeltaTime;
	SetActorLocation(newLocation);
}

언리얼에디터를 끄고 빌드후 언리얼이 로딩되면  BP_Bullet를 뷰포트에 추가하고 Y Rotation을 90도 회전시킨후 플레이해보면 위로 올라가야 합니다.