본문 바로가기

카테고리 없음

마우스버튼입력액션

테스트를 위해 뷰포트에 놓왔던 BP_Bullet는 제거합니다.

총알 발사를 위해 프로젝트 Input탭의 바인딩에 Action을 추가합니다.

총알 발사 위치를 설정하기 위해  PlayerPawn헤더파일에 UArrowComponent 포인터변수를 추가합니다.

총알블루프린트는 레벨에 배치되지 않았지만 실시간으로 생성되므로 TSubclassOf<>라는 자료형으로 선언해야 합니다.

 

	UPROPERTY(EditAnywhere)
	class UArrowComponent* firePosition;

	UPROPERTY(EditAnywhere)
	TSubclassOf<class ABullet> bulletFactory;

생성자에 firePosition객체를 얻어오는 코드를 추가하고 boxComp에 붙여줍니다.

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);

	firePosition = CreateDefaultSubobject<UArrowComponent>(TEXT("Fire Position"));
	firePosition->SetupAttachment(boxComp);
}

빌드후 BP_PlayerPawn을 클릭해서 bulletFactory에 BP_Bullet을 할당합니다.

뷰포트에서 FirePosition을 선택하고 총구방향이 위로 향하도록 Rotation Y를 90로 하고 큐브 위쪽 가운데로 위치를 조정합니다.

이제 마우스 왼쪽 버튼을 누르면 실행할 함수를 만들어 봅니다.playerPawn.h에 Fire()를 추가합니다.

private:
	float h;
	float v;

	void MoveHorizontal(float value);
	void MoveVertical(float value);
	// 총알의 발사 입력 처리함수
	void Fire();
};

playerPawn.cpp에 함수를 구현해줍니다.

void APlayerPawn::Fire()
{
	ABullet* bullet = GetWorld()->SpawnActor<ABullet>(
		bulletFactory, firePosition->GetComponentLocation(), firePosition->GetComponentRotation());
}

bulletFactory는 BP_Bullet이므로 이 블루프린트를 해당위치 회전으로 생성해줍니다. Bullet헤드파일이 include되어야합니다.

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

BindAction()을 추가해 마우스좌클릭을 Fire()함수와 연결해 줍니다. BindAxis와 다른게 Input Event를 추가해 줘야 합니다.

void APlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	PlayerInputComponent->BindAxis("Horizontal", this, &APlayerPawn::MoveHorizontal);
	PlayerInputComponent->BindAxis("Vertical", this, &APlayerPawn::MoveVertical);
	PlayerInputComponent->BindAction("Fire",IE_Pressed, this, &APlayerPawn::Fire);
}

Bullet.cpp
0.00MB
PlayerPawn.cpp
0.00MB
Bullet.h
0.00MB
PlayerPawn.h
0.00MB