프로젝트를 Blank C++ 로 생성한후, Pawn을 c++로 만든다.

생성된 c++을 상속하는 BluePrint를 만든다.

게임모드 베이스를 생성한다.

 

만들어진 게임모드베이스를 상속하여 BT를 만든다.

만들어진 BT에서 디폴드 폰을 다음과 같이 변경한다.

프로젝트 세팅에서 Maps & Modes 에서 다음과 같이 변경한다.

 

그후 World Outliner에서 PlayerStart를 삭제한후 게임을 실행시키면 다음과 같이 생성되는것을 볼 수 있다.

 

MyPawn class 헤더파일에 다음과 같이 추가한다.

 

생성자에 다음과 같이 코딩한다.

빌드후, BT pawn 파일을 엔진에서 열어보면 다음과 같이 생성된 컴퍼넌트들을 볼 수 있다.

적절하게 모양을 바꿔주자

 

 

그후 BT_Pawn 파일을 드레그해 월드에 생성한후 다음과 같이 auto possess player을 설정한다.

 

게임을 플레이하면 pawn이 생성되어 카메라가 붙은것을 확인 할 수 있다.

 

다음과 같이 키 바인딩 한다.

 

다음과 같이 pawn을 코딩한다.

<헤더>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
 
UCLASS()
class LAB5_KONG_WOONHAK_API AMyPawn : public APawn
{
    GENERATED_BODY()
 
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "My Pawn", meta = (AllowPrivateAccess = "true"))
    class UStaticMeshComponent* MeshComponent;
 
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "My Pawn", meta = (AllowPrivateAccess = "true"))
    class UCameraComponent* FollowCamera;
 
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "My Pawn", meta = (AllowPrivateAccess = "true"))
    class USpringArmComponent* SpringArm;
 
 
 
 
public:
    // Sets default values for this pawn's properties
    AMyPawn();
 
    void CalculateForwardInput(float value);
    void CalculateRightInput(float value);
    void CalculateRotationInput(float value);
    void Move();
    void Rotate();
 
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Movement");
    float MoveSpeed = 500;
 
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Movement");
    float RotateSpeed = 100;
 
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Movement");
    FVector MoveForwardDirection;
 
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Movement");
    FVector MoveRightDirection;
 
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Movement");
    FQuat RotationDirection;
 
 
 
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
 
public:
    // Called every frame
    virtual void Tick(float DeltaTime) override;
 
    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
 
};
 
cs

 

<cpp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Fill out your copyright notice in the Description page of Project Settings.
 
 
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
 
// Sets default values
AMyPawn::AMyPawn()
{
     // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
 
    MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
    SetRootComponent(Cast<USceneComponent>(MeshComponent));
 
    SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
    SpringArm->SetupAttachment(RootComponent);
 
    FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
    FollowCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
 
}
 
void AMyPawn::CalculateForwardInput(float value)
{
    MoveForwardDirection= FVector(value * MoveSpeed * GetWorld()->DeltaTimeSeconds, 00);
 
}
 
void AMyPawn::CalculateRightInput(float value)
{
    MoveRightDirection = FVector(0, value * MoveSpeed * GetWorld()->DeltaTimeSeconds, 0);
}
 
void AMyPawn::CalculateRotationInput(float value)
{
    float RotateAmount = value * RotateSpeed * GetWorld()->DeltaTimeSeconds;
    FRotator Rotation = FRotator(0, RotateAmount, 0);
    RotationDirection = FQuat(Rotation);
    //UE_LOG(LogTemp, Warning, TEXT("value  = %f"), value);
}
 
void AMyPawn::Move()
{
    FVector MoveDirection = MoveForwardDirection + MoveRightDirection;
    AddActorLocalOffset(MoveDirection, true);
}
 
void AMyPawn::Rotate()
{
    AddActorLocalRotation(RotationDirection, true);
}
 
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
    Super::BeginPlay();
 
}
 
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
 
    Move();
    Rotate();
}
 
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
 
    PlayerInputComponent->BindAxis("MoveForward"this&AMyPawn::CalculateForwardInput);
    PlayerInputComponent->BindAxis("MoveRight"this&AMyPawn::CalculateRightInput);
    PlayerInputComponent->BindAxis("Turn"this&AMyPawn::CalculateRotationInput);
 
}
 
 
cs

 

그후 게임을 실행하면 이동할 수 있다.

 

+ Recent posts