[C++] 투사체 만들기

Woogle·2022년 11월 17일
0

언리얼 엔진 5

목록 보기
30/59

📄 Bullet.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bullet.generated.h"

UCLASS()
class MYTPS_API ABullet : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ABullet();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

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

	UPROPERTY(EditAnywhere)
	class USphereComponent* sphereCollision;

	UPROPERTY(EditAnywhere)
	class UStaticMeshComponent* meshComp;

	UPROPERTY(EditAnywhere)
	class UProjectileMovementComponent* projectileMoveComp;
};

📄 Bullet.cpp

#include "Bullet.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"

// Sets default values
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;

	sphereCollision = CreateDefaultSubobject<USphereComponent>(TEXT("sphereCollision"));
	sphereCollision->SetSphereRadius(10.f);
	sphereCollision->SetCollisionProfileName(TEXT("BlockAll"));
	RootComponent = sphereCollision;

	meshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("meshComp"));
	ConstructorHelpers::FObjectFinder<UStaticMesh> tempMesh(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
	if (tempMesh.Succeeded())
	{
		meshComp->SetStaticMesh(tempMesh.Object);	// .Object 안에 찾은 물건이 들어있음
		meshComp->SetRelativeScale3D(FVector(0.2f));
		meshComp->SetCollisionProfileName(TEXT("NoCollision"));
	}
	meshComp->SetupAttachment(RootComponent);

	projectileMoveComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("projectileMoveComp"));
	projectileMoveComp->SetUpdatedComponent(sphereCollision);	// 움직일 대상 지정
	projectileMoveComp->InitialSpeed = 5000;
	projectileMoveComp->MaxSpeed = 5000;
	projectileMoveComp->bShouldBounce = true;
	projectileMoveComp->Bounciness = 0.3f;
}

// Called when the game starts or when spawned
void ABullet::BeginPlay()
{
	Super::BeginPlay();
	SetLifeSpan(2.f);	// 수명
}

// Called every frame
void ABullet::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
profile
노력하는 게임 개발자

0개의 댓글