2 - キャラクターを実装する

ファースト パーソン シューター キャラクターを実装する方法を学びます。

Choose your operating system:

Windows

macOS

Linux

前提トピック

このページは以下のトピックへの知識があることを前提にしています。まず以下のトピックの内容についてご確認をお願いします。

コンテンツ

Section0208.gif

このセクションを終了したときに、このような画面になります。

目標

このセクションでは、First Person Shooter キャラクターの実装方法を説明します。

目的

このチュートリアルのこのセクションを完了すると、次のことができるようになります。

  • 新しいキャラクターを作成する

  • 軸マッピングを設定する

  • キャラクター移動関数を実装する

  • マウス カメラ コントロールを実装する

  • キャラクター ジャンプを実装する

  • メッシュをキャラクターに追加する

  • カメラ ビューを変更する

  • ファース トパーソン メッシュをキャラクターに追加する

ステップ

  • 2.1 - 新しいキャラクターを作成する

  • 2.2 - 軸マッピングを設定する

  • 2.3 - キャラクター移動関数を実装する

  • 2.4 - マウス カメラ コントロールを実装する

  • 2.5 - キャラクター ジャンプを実装する

  • 2.6 - メッシュをキャラクターに追加する

  • 2.7 - カメラ ビューを変更する

  • 2.8 - ファースト パーソン メッシュをキャラクターに追加する

2.1 - 新しいキャラクターを作成する

このステップでは、Unreal Engine (UE) でエンジンの Character ベース クラスを使用して、新キャラクターを作成します。Character クラス ( Pawn クラスから派生) には、歩行、走行、跳躍といった二足歩行移動向けのビルトイン機能があります。

Character クラスを追加する

.h および .cpp ファイルを手動で、Visual Studio (VS) ソリューションに追加できますが、便利なのは C++ クラス ウィザードを使用して、新クラスをプロジェクトに追加する方法です。

C++ Class Wizard を使用することで、Unreal Engine では固有のマクロをセットアップするヘッダーとソース テンプレートを作成します。

  1. FPS プロジェクトを UE で起動します (起動していない場合)。

  2. ファイル メニューで [New C++ Class... (新規 C++ クラス)] をクリックして、新しい親クラスを選択します。

    SelectNewCPPClass.png

  3. [Choose Parent Class (親クラスを選択)] メニューが開きます。下にスクロールして、 [Character] を親クラスとして選択し、 [Next (次へ)] をクリックします。

    ChooseCharacterClass.png

  4. 新規クラスに「FPSCharacter」と名前を付けてから、 [Create Class (クラスを作成)] をクリックします。

    MakeFPSCharacterClass.png

Character クラスを検証する

  1. VS の [Solution Explorer] で、 [FPSProject] > [Source] > [FPSProject] の順に展開します。

    ExpandedSolutionExplorer.png

  2. FPSCharacter.cpp をクリックして、 FPSCharacter クラスのための実装ファイルを開きます。

  3. BeginPlay() 関数 ( Super::BeginPlay(); の下) に次のコード行を追加して、 FPSCharacter クラスが使用されていることを検証します。

    check(GEngine != nullptr);
    
    // Display a debug message for five seconds. 
    // The -1 "Key" value argument prevents the message from being updated or refreshed.
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
  4. FPSCharacter.cpp は以下のようになります。

    // Copyright Epic Games, Inc.All Rights Reserved.
    
    #include "FPSCharacter.h"
    
    // Sets default values
    AFPSCharacter::AFPSCharacter()
    {
    // Set this character to call Tick() every frame.You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
    
    }
    
    // Called when the game starts or when spawned
    void AFPSCharacter::BeginPlay()
    {
    Super::BeginPlay();
    
    check(GEngine != nullptr);
    
    // Display a debug message for five seconds. 
    // The -1 "Key" value argument prevents the message from being updated or refreshed.
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
    
    }
    
    // Called every frame
    void AFPSCharacter::Tick(float DeltaTime)
    {
    Super::Tick(DeltaTime);
    
    }
    
    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
    Super::SetupPlayerInputComponent(PlayerInputComponent);
    
    }
  5. Visual Studio に FPSCharacter CPP ファイルを保存します。

  6. [Solution Explorer (ソリューション エクスプローラ)] [FPSProject] を探します。

    ここまで、エディタの [Build (ビルド)] ボタンで、プロジェクトをコンパイルしました。このステップでは、Visual Studio のビルド機能をつかってコードをコンパイルしてみましょう。Visual Studio 内からコードをコンパイルするには、 [FPSProject] 上で 右クリックして、 [Build (ビルド)] を選択し、プロジェクトをコンパイルします。

  7. Visual Studio 内からコードをコンパイルするには、 [FPSProject] を右クリックし、 [Build (ビルド)] を選択して、プロジェクトをコンパイルします。

    BuildProject.png

    デフォルト設定で VS を使用している場合は、プログラムの下の方にダイアログボックスがあります (コードを編集した場所の下)。[Build (ビルド)] をクリックすると、処理中にいくつかのメッセージがダイアログに表示され、通常、最後にビルド完了メッセージが表示されます。 ビルドが失敗しても、心配はありません。ステップを最初から見直して、セクション 1 と 2 に記載された内容にコードが一致しているかどうか確認してください。

  8. ビルド終了後に Unreal Editor を開いて、新しくコンパイルした FPSCharacter クラスが コンテンツ ブラウザ で見えることを確認します。

    FPSCharacterContentBrowser.png

C++ FPS Character クラスをブループリントに拡張する

ここで C++ FPS Character クラスをブループリントに拡張します (前に FPSProject ゲーム モードで実行したものと同様)。C++ クラスからブループリントへの拡張の詳細については、「 C++ とブループリント 」リファレンス ページを参照してください。

  1. FPSCharacter クラスを右クリックして、 [C++ Class Actions] メニューを開きます。

  2. [Create Blueprint class based on FPSCharacter] をクリックして [Add Blueprint Class] ダイアログ メニューを開きます。

    CreateDerivedBPClass.png

  3. 新しい Blueprint クラスに BP_FPSCharacter と名前を付けて、「Blueprints」フォルダを選択してから、 [Create Blueprint Class] ボタンをクリックします。

    AddBPClass.png

  4. ここまでで、新規作成した BP_FPSCharacter Blueprint クラスが「Blueprint」フォルダの中にあるはずです。

    AddedBPClass.png

  5. BP_FPSCharacter ブループリントを保存してからブループリント エディタを閉じるようにします。

デフォルトの Pawn クラスを設定する

新しく変更したゲーム モードをブループリントに拡張したので、このステップで BP_FPSCharacter をデフォルト Pawn として使用するように、プロジェクトを設定する必要があります。

  1. [Edit (編集)] メニューで、 [Project Settings (プロジェクト設定)] をクリックします。

  2. [Project Settings] タブの左側にある [Project (プロジェクト)] セクションで、 [Maps & Modes] をクリックします。

  3. [Selected GameMode (選択したゲームモード)] セクションを展開して、 [BP_FPSCharacter] [Default Pawn Class] ドロップダウン メニューで選択します。

    SettingFPSCharacter.png

  4. [Project Settings] メニューを閉じます。

  5. レベル エディタのツールバー で、 [Play (プレイ)] ボタンをクリックします。ビューポートの左上に 5 秒間、「Hello World, this is FPSGameMode!」が表示され、その下に赤色のテキストで、「We are using FPSCharacter.」が表示されます。

    VerifyingFPSCharacterResult.png

    移動はできませんが、FPSCharacter は Pawn として正しく使用されています。新しい Character にはまだ移動コントロールが何もないので、レベル内を動き回ることはできません。

  6. 次のステップに進む前に、Esc キーを押す、またはレベル エディタの [Stop (停止)] をクリックして、プレイ イン エディタ (PIE) モードから抜け出します。

2.2 - 軸マッピングを設定する

一般的に軸マッピングでは、キーボード、マウス、およびコントローラーの入力を「フレンドリーネーム」にマッピングします。この「フレンドリーネーム」は後でゲーム ビヘイビア (移動など) にバインドすることができます。軸マッピングは継続的にポーリングされ、移動のシームレスな遷移と滑らかなゲーム ビヘイビアを実現します。ハードウェア軸 (コントローラー ジョイスティックなど) では、オンオフの入力 (押された場合は 1 で、いない場合は 0 など) ではなく、入力の度合いが得られます。コントローラー ジョイスティック入力方式は、動きの入力の大きさを取得するのに効果的ですが、軸マッピングでは、一般的な移動キー (W、A、S、D、や矢印キー) を継続的にポーリングされるゲーム ビヘイビアにマップすることもできます。

このステップをさらに進める前にプレイヤー入力を確認するには、「 プレイヤー入力と Pawn クラス 」チュートリアルを参照してください。このステップでは、新しいキャラクターをマップで動き回らせるために、W、A、S、D、の各キーに対する入力軸マッピングを設定します。

MoveForward 軸マッピング

  1. [Edit (編集)] メニューから [Project Settings (プロジェクト設定)] を選択します。

  2. [Project Settings] タブの左側にある [Engine] の見出しで、 [Input] をクリックします。

  3. [Bindings] [Axis Mappings] の隣にあるプラス記号をクリックします。

  4. [Axis Mappings] の左側にある矢印をクリックします。

  5. 表示されるテキスト欄に "MoveForward" と入力し、テキスト ボックスの左側の矢印をクリックして軸のバインディング オプションを展開します。

  6. ドロップダウン メニューで [Keyboard] ドロップダウン リストから [W] を選択します。

  7. 入力設定は以下のようになります。

    MoveForwardAxisMap_W.png

  8. [MoveForward] の隣にあるプラス記号を クリック します。

  9. 2 番目のドロップダウン メニューで、 [Keyboard] ドロップダウン リストから [S] を選択します。

  10. [S] の隣の [Scale] 欄に、「-1」と入力します。

  11. 入力設定は以下のようになります。

    MoveForwardAxisMap_S.png

MoveRight 軸マッピング

  1. [Bindings (バインディング)] [Axis Mappings (軸マッピング)] の隣にある [+] 記号をクリックします。

  2. 表示されるテキスト フィールドに「MoveRight」と入力し、テキスト ボックスの左にある矢印をクリックして、軸バインディング オプションを展開します。

  3. ドロップダウン メニューで [Keyboard] ドロップダウン リストから [D] を選択します。

  4. 入力設定は以下のようになります。

    MoveRightAxisMap_D.png

  5. [MoveRight] の隣にある [+] 記号をクリックします。

  6. 2 番目のドロップダウン メニューで、 [Keyboard] ドロップダウン リストから [A] を選択します。

  7. [A] の隣の Scale フィールドに「-1"」と入力します。

  8. 入力設定は以下のようになります。

    MoveRightAxisMap_A.png

  9. これで MoveLeft と MoveRight の軸マッピングを設定したので、 [Project Settings (プロジェクト設定)] メニューを閉じます。

2.3 - キャラクターに移動関数を実装する

このステップでは、プレイヤー入力コンポーネントを設定して、FPSCharacter クラスで次の関数を実装します。

  • MoveForward

  • MoveRight

Movement 関数のインターフェース

FPSCharacter の軸マッピングを設定したので、VS のプロジェクトに切り替えます。

  1. FPSCharacter.h で、以下の関数の宣言を`SetupPlayerInputComponent` の下に追加します。

    // Handles input for moving forward and backward.
    UFUNCTION()
    void MoveForward(float Value);
    
    // Handles input for moving right and left.
    UFUNCTION()
    void MoveRight(float Value);

    UFUNCTION マクロ (各関数の上にある) により、エンジンがこれらの関数を認識して、シリアル化と他のエンジン機能に含められるようになります。

  2. FPSCharacter.h は以下のようになります。

    // Copyright Epic Games, Inc.All Rights Reserved.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Character.h"
    #include "FPSCharacter.generated.h"
    
    UCLASS()
    class FPSPROJECT_API AFPSCharacter : public ACharacter
    {
        GENERATED_BODY()
    
    public:
        // Sets default values for this character's properties
        AFPSCharacter();
    
    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;
    
        // Handles input for moving forward and backward.
        UFUNCTION()
        void MoveForward(float Value);
    
        // Handles input for moving right and left.
        UFUNCTION()
        void MoveRight(float Value);
    
    };

移動関数の実装

一般的な FPS コントロール スキームでは、キャラクターの移動軸はカメラを基準にしたものになります。「前進」の動きは「カメラが向いている方向」への動きで、「右」は「カメラの向きに対して右方向」への動きです。キャラクターのコントール回転を取得するには、 PlayerController を使用します。さらに、 MoveForward 関数はコントロール回転のピッチ コンポーネントを無視し、入力を XY 平面内に制限します。これは上や下を向いたときに、キャラクターが床から離れずに移動するようにするためです。

  1. FPSCharacter.cpp で、以下の行を Super::SetupPlayerInputComponent(PlayerInputComponent) の下の SetupPlayerInputComponent 関数に追加します。

    // Set up "movement" bindings.
    PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);

    InputComponent は、入力データの処理方法を定義するコンポーネントです。 InputComponent は、入力を受けさせたいアクタにアタッチすることができます。

  2. SetupPlayerInputComponent の下に、次の MoveForward 関数定義 ( FPSCharacter.cpp 内) を追加します。

    void AFPSCharacter::MoveForward(float Value)
    {
        // Find out which way is "forward" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
        AddMovementInput(Direction, Value);
    }
  3. 次の MoveRight 関数定義を FPSCharacter.cpp に追加します ( MoveForward の下) 。

    void AFPSCharacter::MoveRight(float Value)
    {
        // Find out which way is "right" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
        AddMovementInput(Direction, Value);
    }
  4. FPSCharacter.cpp は以下のようになります。

    // Copyright Epic Games, Inc.All Rights Reserved.
    
    #include "FPSCharacter.h"
    
    // Sets default values
    AFPSCharacter::AFPSCharacter()
    {
        // Set this character to call Tick() every frame.You can turn this off to improve performance if you don't need it.
        PrimaryActorTick.bCanEverTick = true;
    
    }
    
    // Called when the game starts or when spawned
    void AFPSCharacter::BeginPlay()
    {
        Super::BeginPlay();
    
        check(GEngine != nullptr);
    
          // Put up a debug message for five seconds. 
        // The -1 "Key" value argument prevents the message from being updated or refreshed.
          GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
    
    }
    
    // Called every frame
    void AFPSCharacter::Tick( float DeltaTime )
    {
        Super::Tick( DeltaTime );
    
    }
    
    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    }
    
    void AFPSCharacter::MoveForward(float Value)
    {
        // Find out which way is "forward" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
        AddMovementInput(Direction, Value);
    }
    
    void AFPSCharacter::MoveRight(float Value)
    {
        // Find out which way is "right" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
        AddMovementInput(Direction, Value);
    }

キャラクターの移動をテストする

新規で実装したキャラクターの移動関数をコンパイルし、テストします。

  1. FPSCharacter ヘッダ (.h) と C++ (.cpp) ファイルを VS で保存します。

  2. [Solution Explorer (ソリューション エクスプローラ)] [FPSProject] を探します。

  3. [FPSProject] を右クリックして [Build] を選択してプロジェクトをコンパイルします。

    BuildProject.png

  4. ビルドが終了したら Unreal Editor で FPSProject を開きます。

  5. レベル エディタのツールバーで [Play (プレイ)] をクリックします前方、後方、左右への移動ができるようになっています。

  6. Esc キーを押す、またはレベル エディタで [Stop (停止)] をクリックして、PIE モードを終了します。

2.4 - マウスによるカメラ制御の実装

このステップでは、マウスで操作して、キャラクターが周囲を見回す機能を追加します。

Turn 軸マッピング

  1. FPS プロジェクトで、 [Edit (編集)] メニューを展開して、 [Project Settings (プロジェクト設定)] をクリックします。

  2. [Project Settings] タブの左側にある [Engine] の見出しで、 [Input] をクリックします。

  3. [Bindings] [Axis Mappings] の隣にある [+] 記号をクリックします。

  4. [Axis Mappings] の左側にある矢印をクリックします。

  5. 表示されたテキスト フィールドに「Turn」と入力し、テキストボックスの左にある矢印をクリックして、軸バインディング オプションを展開します。

  6. ドロップダウン メニューで [Mouse] ドロップダウン リストから [Mouse X] を選択します。

  7. 入力設定は以下のようになります。

    TurnAxisMapping_MouseX.png

軸マッピングのルックアップ

  1. [Bindings] [Axis Mappings] の隣にある [+] 記号をクリックします。

  2. 表示されたテキスト フィールドに「LookUp」と入力し、テキストボックスの左にある矢印をクリックして、軸バインディング オプションを展開します。

  3. ドロップダウン メニューで [Mouse] ドロップダウン リストから [Mouse Y] を選択します。

  4. [Mouse Y] の隣の [Scale] フィールドに「-1.0」と入力します。

  5. 入力設定は以下のようになります。

    TurnAxisMapping_MouseY.png

  6. [Project Settings] メニューを閉じます。

入力処理を実装する

このタイミングで回転や見上げるためのマウス入力を処理するためのコードを追加します。 Character 基本クラスでは、次の 2 つの必須関数を定義します。

  • AddControllerYawInput

  • AddControllerPitchInput

    感度や軸の反転に対する追加サポートなど、追加処理が必要な場合、独自の関数を準備して、入力値を関数に渡す前に調整することもできます。ただしこの場合は、入力を直接 AddControllerYawInput AddControllerPitchInput 関数にバインドします。

  1. 次のコード行を SetupPlayerInputComponent 関数 ( FPSCharacter.cpp 内) に追加します。

    // Set up "look" bindings.
    PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
    PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
  2. SetupPlayerInputComponent 関数は以下のようになります。

    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    
        // Set up "look" bindings.
        PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
        PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
    }

マウス カメラ コントロールをテストする

  1. FPSCharacter 実装ファイルを VS で保存します。

  2. [Solution Explorer (ソリューション エクスプローラ)] [FPSProject] を探します。

  3. [FPSProject] を右クリックして [Build] を選択してプロジェクトをコンパイルします。

  4. ビルドが終了したら Unreal Editor で FPSProject を開きます。

  5. レベル エディタのツールバーで [Play (プレイ)] をクリックしますこれでマウス操作によるカメラの制御が可能になりました。

  6. Esc キーを押す、またはレベル エディタで [Stop (停止)] をクリックして、PIE モードを終了します。

2.5 - キャラクター ジャンプを実装する

一般に、アクション マッピングはオンオフのイベントに対して入力を処理します。これにより入力を「フレンドリ名」にマップして、イベント駆動ビヘイビアにバインドできるようにします。その結果、キー、マウス ボタン、キーパッド ボタンを押して放す、あるいいずれかの操作によって、直接ゲーム ビヘイビアをトリガーできるようになります。このステップでは、キャラクターがジャンプする機能を追加するために、スペース キーの入力アクション マッピングを設定します。

ジャンプ アクション マッピング

  1. [Edit (編集)] メニューから [Project Settings (プロジェクト設定)] を選択します。

  2. [Project Settings] タブの左側にある [Engine] の見出しで、 [Input] をクリックします。

  3. [Bindings] で、 [Action Mappings] の隣にある [+] 記号をクリックします。

  4. [Action Mappings (アクションマッピング)] の左にある矢印をクリックします。

  5. 表示されるテキスト欄に "Jump" と入力し、テキスト ボックスの左側の矢印をクリックしてアクション バインディングのオプションを展開します。

  6. ドロップダウン メニューで [Keyboard] ドロップダウン リストから [Space Bar] を選択します。

  7. 入力設定は以下のようになります。

    JumpActionMapping_SpaceBar.png

  8. [Project Settings] メニューを閉じます。

入力処理を実装する

インターフェース ファイル (.h) の内部で ACharacter ベース クラスを確認すると、キャラクターのジャンプに対するビルトイン サポートがあることがわかると思います。キャラクター ジャンプは bPressedJump 変数に結び付けられ、つまり必要なことは、ジャンプ アクションが押されたときに、boolean を true にして、ジャンプ アクションが放されたときに false にします。これを実行するには次の 2 つの関数を追加する必要があります。

  • StartJump

  • StopJump

Visual Studio に戻り、コードを FPSCharacter クラスに追加します。

  1. FPSCharacter.h で、以下のパブリック関数宣言を追加します。

    // Sets jump flag when key is pressed.
    UFUNCTION()
    void StartJump();
    
    // Clears jump flag when key is released (キー解放時のジャンプフラグをクリア)
    UFUNCTION()
    void StopJump();
  2. FPSCharacter.h は以下のようになります。

    // Copyright Epic Games, Inc.All Rights Reserved.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Character.h"
    #include "FPSCharacter.generated.h"
    
    UCLASS()
    class FPSPROJECT_API AFPSCharacter : public ACharacter
    {
        GENERATED_BODY()
    
    public:
        // Sets default values for this character's properties
        AFPSCharacter();
    
    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;
    
        // Handles input for moving forward and backward.
        UFUNCTION()
        void MoveForward(float Value);
    
        // Handles input for moving right and left.
        UFUNCTION()
        void MoveRight(float Value);
    
        // Sets jump flag when key is pressed.
        UFUNCTION()
        void StartJump();
    
        // Clears jump flag when key is released.
        UFUNCTION()
        void StopJump();
    };
  3. ここで、次のコードを SetupPlayerInputComponent に追加して、ジャンプ アクションを新しく記述した関数にバインドします。

    void AFPSCharacter::StartJump()
    {
        bPressedJump = true;
    }
    
    void AFPSCharacter::StopJump()
    {
        bPressedJump = false;
    }
  4. 以下のコードを SetupPlayerInputComponent に追加し、ジャンプ アクションを新しく記述した関数にバインドします。

    // Set up "action" bindings.
    PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
    PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
  5. FPSCharacter.cpp は以下のようになります。

    // Copyright Epic Games, Inc.All Rights Reserved.
    
    #include "FPSCharacter.h"
    
    // Sets default values
    AFPSCharacter::AFPSCharacter()
    {
        // Set this character to call Tick() every frame.You can turn this off to improve performance if you don't need it.
        PrimaryActorTick.bCanEverTick = true;
    
    }
    
    // Called when the game starts or when spawned
    void AFPSCharacter::BeginPlay()
    {
        Super::BeginPlay();
    
        check(GEngine != nullptr);
    
          // Display a debug message for five seconds. 
        // The -1 "Key" valueargument prevents the message from being updated or refreshed.
          GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
    
    }
    
    // Called every frame
    void AFPSCharacter::Tick( float DeltaTime )
    {
        Super::Tick( DeltaTime );
    
    }
    
    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    
        // Set up "look" bindings.
        PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
        PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
    
        // Set up "action" bindings.
        PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
        PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
    }
    
    void AFPSCharacter::MoveForward(float Value)
    {
        // Find out which way is "forward" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
        AddMovementInput(Direction, Value);
    }
    
    void AFPSCharacter::MoveRight(float Value)
    {
        // Find out which way is "right" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
        AddMovementInput(Direction, Value);
    }
    
    void AFPSCharacter::StartJump()
    {
        bPressedJump = true;
    }
    
    void AFPSCharacter::StopJump()
    {
        bPressedJump = false;
    }

キャラクター ジャンプをテストする

新規で実装したキャラクターの移動関数をコンパイルし、テストします。

  1. FPSCharacter ヘッダ (.h) と CPP (.cpp) ファイルを VS で保存します。

  2. [Solution Explorer (ソリューション エクスプローラ)] [FPSProject] を探します。

  3. [FPSProject] を右クリックして [Build] を選択してプロジェクトをコンパイルします。

  4. ビルドが終了したら Unreal Editor で FPSProject を開きます。

  5. レベル エディタのツールバーで [Play (プレイ)] をクリックしますマップ全体でジャンプできるようになります。

  6. Esc キーを押す、またはレベル エディタで **[Stop (停止)] をクリックして、PIE モードを終了します。

2.6 - キャラクターにメッシュを追加する

次のリンクからサンプル メッシュをダウンロードして展開してください。

このステップでは、キャラクターにスケルタル メッシュを加えます。Character クラスはデフォルトで SkeletalMeshComponent オブジェクトを作成します。つまり必要なのは、どの SkeletalMesh アセットを使用するのかということです。

スケルタルメッシュをインポートする

  1. コンテンツ ブラウザのファイル ボックス内の「Content」フォルダに戻ります。

  2. コンテンツ ブラウザのファイル ボックス内で右クリックして [Import Asset (アセットのインポート)] ダイアログボックスを開きます。

  3. 'Import to /Game...' をクリックして [Import (インポート)] ダイアログボックスを開きます。

  4. GenericMale.fbx 」メッシュ ファイルを探して選択します。

  5. プロジェクトへのメッシュのインポートを開始するには、 [Open (開く)] をクリックします。

  6. コンテンツ ブラウザ に、 [FBX Import Options (FBXインポートオプション)] ダイアログ ボックスが表示されます。 [Import All (すべてインポート)] をクリックすると、メッシュがプロジェクトに追加されます。

    ImportGenericMale.png

  7. [Save (保存)] ボタンをクリックしてインポートしたメッシュを保存します。

サードパーソン メッシュを設定する

  1. BP_FPSCharacter ブループリント クラス アイコンを「 Content (コンテンツ) 」 > 「 Blueprints 」でダブルクリックして、 ブループリント エディタ で開きます。

    このブループリントがデータ専用ブループリントであると表示されている場合は、 [Open Full Blueprint Editor (フルブループリントエディタを開く)] をクリックします。

  2. [Components] タブで Mesh コンポーネントをクリックします。

    MeshComponent.png

  3. [Details (詳細)] タブの [Mesh (メッシュ)] セクションにスクロールします (画面の右側にない場合は、 [Window (ウィンドウ)] > [Details (詳細)] を選択)。

  4. Skeletal Mesh (スケルタル メッシュ) 行の [None (なし)] というドロップダウンをクリックして、 [GenericMale] スケルタル メッシュを選択します。

    SelectSkeletalMesh.png

  5. [Details (詳細)] ペインの [Transform (トランスフォーム)] セクションにスクロールして、 SkeletalMeshComponent CapsuleComponent に合わせるために、 Z 位置を「 -88.0 」に設定します。

    NewZLoc_Mesh.png

  6. ビューポートを開いて、スケルタル メッシュをプレビューします。次のようになります。

    SkeletalMeshLocation.png

    スケルタル メッシュが CapsuleComponent 内部にあり、メッシュが ArrowComponent と同じ方向を向いていることを確認します。スケルタル メッシュ コンポーネントの向きを正しく設定することで、キャラクターがワールド内を正しく移動するようになります。

  7. BP_FPSCharacter ブループリントを Compile (コンパイル) および Save (保存) してから、 ブループリント エディタ を閉じてください。

新しいメッシュを PIE モードで検証する

ここで、新しく追加されたメッシュをエディタ内で表示します。

  1. レベル エディタのツールバー で、 [Play (プレイ)] ボタンをクリックします。移動するにつれて、キャラクターのシャドウが表示されるはずです。

    PawnwithShadow.png

    Section0206.gif

    エディタのビューポート内でキャラクターのメッシュを確認する場合は、F8 キーを押して、ポーンから自身を切り離します。F8 キーを押した後に、レベルでカメラを自由に動かすことができます。カメラを動かすには、マウスの左ボタンを押したまま、マウスを動かします。

    FreelyMovingCamera.png

  2. プレイ イン エディタ (PIE) モードを終了するには、レベル エディタで Escape キー を押すか、 [Stop] ボタンをクリックします。

2.7 - カメラ ビューを変更する

前のステップの最後で、デフォルトのカメラはメッシュの首に配置されていました。このステップでは、カメラのプロパティ (位置や視野角など) を調整するために使用する FPS カメラを設定します。 開始する前に、 FPSCharacter.h にインクルードされたファイルのリストを展開する必要があります。これにより、コードでさらに多くのカメラ関連関数にアクセスできるようになり、最終的にカメラの配置を操作できます。

  1. Visual Studio プロジェクトを開き、 FPSCharacter.h に移動します。

  2. 次のヘッダ ファイルを FPSCamera.h にインクルードします。

    #include "Camera/CameraComponent.h"
    #include "Components/CapsuleComponent.h"

Camera コンポーネントをアタッチする

  1. FPSCharacter.h を開いて、次のコードを追加します。

    // FPS camera.
    UPROPERTY(VisibleAnywhere)
    UCameraComponent* FPSCameraComponent;
  2. FPSCharacter.h は以下のようになります。

    // Copyright Epic Games, Inc.All Rights Reserved.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Character.h"
    #include "Camera/CameraComponent.h"
    #include "Components/CapsuleComponent.h"
    #include "FPSCharacter.generated.h"
    
    UCLASS()
    class FPSPROJECT_API AFPSCharacter : public ACharacter
    {
    GENERATED_BODY()
    
    public:
    // Sets default values for this character's properties
    AFPSCharacter();
    
    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;
    
    // Handles input for moving forward and backward.
    UFUNCTION()
    void MoveForward(float Value);
    
    // Handles input for moving right and left.
    UFUNCTION()
    void MoveRight(float Value);
    
    // Sets jump flag when key is pressed.
    UFUNCTION()
    void StartJump();
    
    // Clears jump flag when key is released.
    UFUNCTION()
    void StopJump();
    
    // FPS camera
    UPROPERTY(VisibleAnywhere)
    UCameraComponent* FPSCameraComponent;
    };
  3. FPSCharacter.cpp を開き、次のコードをコンストラクタの PrimaryActorTick.bCanEverTick = true: の下に追加します。

    // Create a first person camera component.
    FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
    check(FPSCameraComponent != nullptr);
    
    // Attach the camera component to our capsule component.
    FPSCameraComponent->SetupAttachment(CastChecked<USceneComponent, UCapsuleComponent>(GetCapsuleComponent()));

    前述のコードは、 UCameraComponent を作成し、それをキャラクターの`CapsuleComponent` にアタッチします。

  4. ここで、コンストラクタで記述したブロックの下に次のコードを追加します。

    // Position the camera slightly above the eyes.
    FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));
    
    // Enable the pawn to control camera rotation.
    FPSCameraComponent->bUsePawnControlRotation = true;

    このコードは、ポーンでカメラの回転をコントロールできるようにしながら、キャラクターの目の少し上にカメラの位置を調整します。

  5. FPSCharacter.cpp は以下のようになります。

    // Copyright Epic Games, Inc.All Rights Reserved.
    
    #include "FPSCharacter.h"
    
    // Sets default values
    AFPSCharacter::AFPSCharacter()
    {
        // Set this character to call Tick() every frame.You can turn this off to improve performance if you don't need it.
        PrimaryActorTick.bCanEverTick = true;
    
        // Create a first person camera component.
        FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
        check(FPSCameraComponent != nullptr);
    
        // Attach the camera component to our capsule component.
        FPSCameraComponent->SetupAttachment(CastChecked<USceneComponent, UCapsuleComponent>(GetCapsuleComponent()));
    
        // Position the camera slightly above the eyes.
        FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));
    
        // Enable the pawn to control camera rotation.
        FPSCameraComponent->bUsePawnControlRotation = true;
    }
    
    // Called when the game starts or when spawned
    void AFPSCharacter::BeginPlay()
    {
        Super::BeginPlay();
    
        check(GEngine != nullptr)
    
          // Display a debug message for five seconds. 
         // The -1 "Key" value (first argument) indicates that we will never need to update or refresh this message.
          GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
    
    }
    
    // Called every frame
    void AFPSCharacter::Tick( float DeltaTime )
    {
        Super::Tick( DeltaTime );
    
    }
    
    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    
        // Set up "look" bindings.
        PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
        PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
    
        // Set up "action" bindings.
        PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
        PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
    }
    
    void AFPSCharacter::MoveForward(float Value)
    {
        // Find out which way is "forward" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
        AddMovementInput(Direction, Value);
    }
    
    void AFPSCharacter::MoveRight(float Value)
    {
        // Find out which way is "right" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
        AddMovementInput(Direction, Value);
    }
    
    void AFPSCharacter::StartJump()
    {
        bPressedJump = true;
    }
    
    void AFPSCharacter::StopJump()
    {
        bPressedJump = false;
    }

新規カメラをテストする

ここでは、新しく実装されたカメラ コードをコンパイルしてテストします。

  1. FPSCharacter ヘッダ (.h) と CPP (.cpp) ファイルを Visual Studio で保存します。

  2. [Solution Explorer (ソリューション エクスプローラ)] [FPSProject] を探します。

  3. [FPSProject] を右クリックして [Build] を選択してプロジェクトをコンパイルします。

  4. ビルドが終了したら Unreal Editor で FPSProject を開きます。

  5. レベル エディタのツールバーで [Play (プレイ)] をクリックします

これで、カメラはキャラクターの頭の少し上に位置するようになります。

BP_FPSCharacter のビューポートを開いて、新しく追加されたカメラ コンポーネントを確認することもできます。 NewCameraComponent.png

NewCameraPosition.png

2.8 - ファースト パーソン メッシュをキャラクターに追加する

FPS ゲーム作成での一般的なアプローチとして、フルボディ メッシュと「武器と腕」メッシュの 2 つの別々のキャラクター メッシュを使用します。フルボディ メッシュは第三者の視点からキャラクターを見るために使用しますが、プレイヤー自身の視点で見ているときは表示されません。「武器と腕」メッシュは一般にカメラにアタッチされ、プレイヤー自身の視点でマップを見ているときに、プレイヤーにだけ見えるものです。このステップでは、ファースト パーソン メッシュをキャラクターに追加します。

ファースト パーソン キャラクター メッシュを追加する

  1. Visual Studio に戻り、 FPSCharacter.h を開いて、次のコードを public: の下に追加します。

    // First-person mesh (arms), visible only to the owning player.
    UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
    USkeletalMeshComponent* FPSMesh;
  2. ここで FPSCharacter.cpp を開き、コンストラクタで、次のコードを追加し、ファースト パーソン メッシュを作成し設定します。

    // Create a first person mesh component for the owning player.
    FPSMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FirstPersonMesh"));
    check(FPSMesh != nullptr);
    
    // Only the owning player sees this mesh.
    FPSMesh->SetOnlyOwnerSee(true);
    
    // Attach the FPS mesh to the FPS camera.
    FPSMesh->SetupAttachment(FPSCameraComponent);
    
    // Disable some environmental shadows to preserve the illusion of having a single mesh.
    FPSMesh->bCastDynamicShadow = false;
    FPSMesh->CastShadow = false;

    SetOnlyOwnerSee で示しているのは、このメッシュが、このキャラクターを所有している PlayerController のみから見えるということです。このコードはこのメッシュをカメラにアタッチし、一部の環境シャドウイングも無効にしています。腕がシャドウを投げかけられるようにしてしまうと、ファースト パーソン キャラクターが単一メッシュであるという錯覚が損なわれます。

  3. 最後に、次のコードを FPSCharacter.cpp のコンストラクタに追加して、既存のサードパーソン メッシュをキャラクターの所有者から隠します。

    // The owning player doesn't see the regular (third-person) body mesh.
    GetMesh()->SetOwnerNoSee(true);
  4. FPSCharacter ヘッダ (.h) と実装 (.cpp) ファイルを Visual Studio で保存します。

  5. [Solution Explorer (ソリューション エクスプローラ)] [FPSProject] を探します。

  6. [FPSProject] を右クリックして [Build] を選択してプロジェクトをコンパイルします。

  7. ビルドが終了したら PIE モードで、 FPSProject を開いて実行します。

    HiddenMeshInGame.png

この時点で、キャラクターのメッシュがエディタ内で可視状態になります。

メッシュと投げかけるシャドウがまだ見える場合は、エディタを閉じて再起動します。

メッシュのブループリントを構築する

先に進む前に、「 「ファースト パーソン スケルタル メッシュ」 」からサンプル メッシュをダウンロードして展開してください。

  1. コンテンツ ブラウザのファイル ボックス内で右クリックして [Import Asset (インポート アセット)] ダイアログボックスを開きます。

  2. [Import to /Game...] をクリックして [Import (インポート)] ダイアログボックスを開きます。

    RightClickImport.png

  3. HeroFPP.fbx メッシュ ファイルを探して選択します。

  4. プロジェクトへのメッシュのインポートを開始するには、 [Open (開く)] をクリックします。

  5. コンテンツ ブラウザ に、 [FBX Import Options (FBXインポートオプション)] ダイアログ ボックスが表示されます。

  6. [Skeleton] [None (なし)] に設定されていることを確認してから、 [Import All (すべてインポート)] をクリックします。

    ClearSkeletonMesh.png

  7. 次のメッセージ ログを閉じます。

    FBXWarning.png

    このメッシュではファースト パーソン メッシュ セットアップがまだ表示されます。後のセクションでセットアップするアニメーションと連動します。

  8. [Save (保存)] をクリックして、インポートされたメッシュを保存します。

  9. コンテンツ ブラウザ の「 Blueprints 」フォルダに戻ります。

  10. BP_FPSCharacter のアイコンをダブルクリックして ブループリント エディタ 内で開きます。

  11. 新しい [FPSMesh] コンポーネントを [Components (コンポーネント)] タブで見つけます。場合により フルブループリント エディタ を先に開きます。

    LocateFPSMeshComponent.png

    FPSMesh コンポーネントは FPSCameraComponent, の子であるため、常にカメラにアタッチされます。

  12. [FPSMesh] [Components (コンポーネント)] メニューでクリックします。

  13. [Details (詳細)] タブの [Mesh] セクションへスクロールして、"None" と表示されているドロップダウン メニューをクリックします。 HeroFPP スケルタルメッシュを選択し、 ビューポート に腕を追加します。

    SelectHeroFPPSkeletalMesh.png

  14. 新たに追加した HeroFPP スケルタルメッシュは、 ビューポート 内で次のようになります。

    HeroFPPMesh_Viewport.png

  15. 新しく追加したメッシュのトランスフォームを調整して、 [Location (位置)] を {220, 0, 35} に、 [Rotation (回転)] を {180, 50, 180} に設定します。

    画像をクリックするとズームインします。

  16. BP_FPSCharacter ブループリントを Compile (コンパイル) および Save (保存) してから、 ブループリント エディタ を閉じてください。

新規メッシュをゲーム内で確認する

  1. レベル エディタのツールバー [Play (プレイ)] ボタンをクリックして、ゲーム内で新規メッシュを表示します。

    Section0208.gif

  2. プレイ イン エディタ (PIE) モードを終了するには、レベル エディタで Escape キー を押すか、 [Stop] ボタンをクリックします。

このセクションで完了したコード

FPSCharacter.h

    // Copyright Epic Games, Inc.All Rights Reserved.

    #pragma once

    #include "GameFramework/Character.h"
    #include "Camera/CameraComponent.h"
    #include "Components/CapsuleComponent.h"
    #include "FPSCharacter.generated.h"

    UCLASS()
    class FPSPROJECT_API AFPSCharacter : public ACharacter
    {
        GENERATED_BODY()

    public:
        // Sets default values for this character's properties
        AFPSCharacter();

    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;

        // Handles input for moving forward and backward.
        UFUNCTION()
        void MoveForward(float Value);

        // Handles input for moving right and left.
        UFUNCTION()
        void MoveRight(float Value);

        // Sets jump flag when key is pressed.
        UFUNCTION()
        void StartJump();

        // Clears jump flag when key is released.
        UFUNCTION()
        void StopJump();

        // FPS camera
        UPROPERTY(VisibleAnywhere)
        UCameraComponent* FPSCameraComponent;

        // First-person mesh (arms), visible only to the owning player.
        UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
        USkeletalMeshComponent* FPSMesh;
    };

FPSCharacter.cpp

    // Copyright Epic Games, Inc.All Rights Reserved.

    #include "FPSCharacter.h"

    // Sets default values
    AFPSCharacter::AFPSCharacter()
    {
        // Set this character to call Tick() every frame.You can turn this off to improve performance if you don't need it.
        PrimaryActorTick.bCanEverTick = true;

        // Create a first person camera component.
        FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
        check(FPSCameraComponent != nullptr);

        // Attach the camera component to our capsule component.
        FPSCameraComponent->SetupAttachment(CastChecked<USceneComponent>(GetCapsuleComponent()));

        // Position the camera slightly above the eyes.
        FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));

        // Enable the pawn to control camera rotation.
        FPSCameraComponent->bUsePawnControlRotation = true;

        // Create a first person mesh component for the owning player.
        FPSMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FirstPersonMesh"));
        check(FPSMesh != nullptr);

        // Only the owning player sees this mesh.
        FPSMesh->SetOnlyOwnerSee(true);

        // Attach the FPS mesh to the FPS camera.
        FPSMesh->SetupAttachment(FPSCameraComponent);

        // Disable some environmental shadowing to preserve the illusion of having a single mesh.
        FPSMesh->bCastDynamicShadow = false;
        FPSMesh->CastShadow = false;

        // The owning player doesn't see the regular (third-person) body mesh.
        GetMesh()->SetOwnerNoSee(true);
    }

    // Called when the game starts or when spawned
    void AFPSCharacter::BeginPlay()
    {
        Super::BeginPlay();

        check(GEngine != nullptr);

          // Display a debug message for five seconds. 
        // The -1 "Key" value argument prevents the message from being updated or refreshed.
          GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));

    }

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

    }

    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);

        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);

        // Set up "look" bindings.
        PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
        PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);

        // Set up "action" bindings.
        PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
        PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
    }

    void AFPSCharacter::MoveForward(float Value)
    {
        // Find out which way is "forward" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
        AddMovementInput(Direction, Value);
    }

    void AFPSCharacter::MoveRight(float Value)
    {
        // Find out which way is "right" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
        AddMovementInput(Direction, Value);
    }

    void AFPSCharacter::StartJump()
    {
        bPressedJump = true;
    }

    void AFPSCharacter::StopJump()
    {
        bPressedJump = false;
    }

これで終了です。ここでは、以下の方法について学習しました。

✓ キャラクターの新規作成
✓ 軸マッピングの設定
✓ キャラクターの Movement 関数の実装
✓ マウスによるカメラ制御の実装
✓ キャラクターのジャンプの実装
✓ キャラクターにメッシュを追加
✓ カメラビューの変更
✓ ファーストパーソン メッシュをキャラクターに追加

これで次のセクションで発射物の実装について学ぶ準備が整いました。

Unreal Engine のドキュメントを改善するために協力をお願いします!どのような改善を望んでいるかご意見をお聞かせください。
調査に参加する
閉じる