入力を設定する

ユーザー入力をセットアップする操作手順

実装方法を選んでください。

Blueprints

C++

このページでは、キャラクター を作成し、入力を受けとるようにセットアップします。その後、キャラクターを ゲームモード に割り当て、ゲームプレイ中にデフォルトのポーンになるようにします。キャラクターを作成した後に、プレイヤーの入力にどのように反応するのかを定義します。

Unreal は幅広いプロジェクト タイプにさらに複雑な入力マッピングを提供します。詳細については「Enhanced Input」を参照してください。

プロジェクトのセットアップ

  1. まず、[Games (ゲーム)] > [Blank (空白)] > [Blueprint (ブループリント)] の順に選択して、「SettingUpInput」という名前の新規プロジェクトを作成します。

  2. エディタで、[Edit (編集)] > [Project Settings (プロジェクト設定)] > [Input (入力)] > [Bindings (バインディング)] に移動します。

    project-settings

  1. まず、[Games (ゲーム)] > [Blank (空白)] > [C++] の順に選択して、「SettingUpInput」という名前の新規プロジェクトを作成します。

  2. エディタ で、[Edit (編集)] > [Project Settings (プロジェクト設定)] > [Input (入力)] > [Bindings (バインディング)] に移動します。

project-settings

Action Mappings と Axis Mappings のセットアップ

入力の設定は、Action MappingsAxis Mappings のユーザー定義による Bindings によって行われます。入力動作とそれを呼び出すキーの間に間接のレイヤーを挿入することで、キーと軸を入力動作にうまくマッピングするメカニズムを提供します。

Action Mappings はキーを押したり離したりするためのもので、一方 Axis Mappings は連続した範囲への入力を可能にします。マッピングを定義したら、それらをブループリントまたは C++ の動作にバインドできます。

  1. [Action Mappings] の横にある 追加マーク (+) をクリックして、「Jump」という名前のアクションを新規作成します。

    action-mapping-jump

  2. ドロップダウンの矢印 (1) または キー値選択 ボタン (2) のいずれかから [Space Bar] キー値を探して選択します。

    key-map-select

  3. Axis mappings に移動して 追加マーク (+) をクリックして次の Axis Mapping 名、キー 値、スケール 値を作成します。

    Axis Mapping 名

    キー値

    スケール値

    MoveForward

    W

    1.0

    S

    -1.0

    MoveRight

    A

    -1.0

    D

    1.0

    Turn

    Mouse X

    1.0

    LookUp

    Mouse Y

    -1.0

    input-settings

サンプル キャラクターを作成する

サンプル キャラクターを作成する

Character (キャラクター) は、歩き回る能力を持つ特別なタイプの**ポーン** です。Character は Pawn クラスから拡張され、ワールド内のプレイヤーや AI エンティティの物理表現など、同様のプロパティを継承しています。

その他のドキュメントについては、「Pawn および 入力」を参照してください。

  1. コンテンツ ドロワー から [Add**(**+**)] をクリックし、[Pick Parent Class (親クラスを選択)] メニューから親クラスとして [Character]** を選択します。

    blueprint-choose-parent-class

  2. ブループリントに「BP_ExampleCharacter」という名前を付け、ダブルクリックして、そのクラスのデフォルトを開きます。

Character (キャラクター) は、歩き回る能力を持つ特別なタイプの**ポーン** です。Character は Pawn クラスから拡張され、ワールド内のプレイヤーや AI エンティティの物理表現など、同様のプロパティを継承しています。

その他のドキュメントについては、「Pawn および 入力」を参照してください。

  1. コンテンツ ドロワー**で、「C++ classes」フォルダに移動して右クリックし、[New C++Class (新規 C++ クラス)] を選択して、親クラスとして [Character]** を選択します。

    choose-parent-class

  2. Character クラスに「ExampleCharacter」という名前を付け、[Create Class (クラスを作成)] をクリックします。

![name-your-new-character](NameYourCharacter.png)(w:600)

Creating the SpringArm and Camera Components

SpringArm コンポーネントと Camera コンポーネントを作成する

Camera コンポーネントと **SpringArm コンポーネント**を一緒に使用すると、ゲーム ワールドに合わせて動的に調整できるサードパーソン視点向けの機能が提供されます。Camera (カメラ) コンポーネントには、プレイヤーの視点、つまり、プレイヤーがどのようにワールドを見ているのかを表すカメラが含まれています。SpringArm コンポーネントは、プレイヤーのカメラがワールドに衝突しないようにするための「カメラ ブーム」として使用されます。

  1. [Components (コンポーネント)] タブで、[Add(+) (追加)] をクリックして、ドロップダウンで「Spring Arm」を検索します。Spring Arm コンポーネントの名前を「CameraBoom」に変更します。

    add-spring-arm-component

  2. 手順 1 を繰り返します。ただし、「Camera」を検索します。Camera コンポーネントの名前を「FollowCamera」に変更します。

    add-follow-camera

  3. [Components (コンポーネント)] タブで FollowCameraCameraBoom 上にドラッグし、アタッチします。

    attaching-the-follow-camera

  4. [Components] タブで [CameraBoom] を選択して、[Details (詳細)] > [Camera Settings (カメラの設定)] に移動して、チェックボックスをクリックして、Use Pawn Control Rotation 変数を有効にします。有効にすると、カメラの親はポーン (ExampleCharacter) のビュー/コントロール回転を使用します。

    using-pawn-control-rotation

    キャラクターの視点に使用できる追加のスプリング アームおよびカメラ設定については、「カメラを使用する」を参照してください。

  5. コンパイル して 保存 します。

    compile-and-save

Camera コンポーネントと **SpringArm コンポーネント**を一緒に使用すると、ゲーム ワールドに合わせて動的に調整できるサードパーソン視点向けの機能が提供されます。Camera (カメラ) コンポーネントには、プレイヤーの視点、つまり、プレイヤーがどのようにワールドを見ているのかを表すカメラが含まれています。SpringArm コンポーネントは、プレイヤーのカメラがワールドに衝突しないようにするための「カメラ ブーム」として使用されます。

  1. コード エディタで、ExampleCharacter.h に移動します。[Class Defaults (クラスのデフォルト)]** で、次のクラスを宣言します。

        protected:
        UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Components")
        class USpringArmComponent* CameraBoom;
    
        UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Components")
        class UCameraComponent* FollowCamera; |

    UProperty 指定子 は、ブループリント エディタでコンポーネントの可視性を提供するために使用されます。

  2. ExampleCharacter.cpp」ファイルに移動します。次のライブラリを include 行に追加します。

        #include "GameFramework/SpringArmComponent.h"
        #include "Camera/CameraComponent.h" 
  3. 次に、AExampleCharacter コンストラクタで以下を実装します。

        AExampleCharacter::AExampleCharacter()
        {
        //Initialize the Camera Boom    
        CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
    
        //Setup Camera Boom attachment to the Root component of the class
        CameraBoom->SetupAttachment(RootComponent);
    
        //Set the boolean to use the PawnControlRotation to true.
        CameraBoom->bUsePawnControlRotation = true;
    
        //Initialize the FollowCamera
        FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
    
        //Set FollowCamera attachment to the Camera Boom
        FollowCamera->SetupAttachment(CameraBoom);
        }

    このコンポーネントは「FObjectInitializer::CreateDefaultSubobject」テンプレートを呼び出し、 SetupAttachment メソッドを使用して親の Scene コンポーネントにアタッチされます。カメラ ブームがポーンの コントロール回転 を使用するように設定すると、自身の回転ではなく、その親ポーンの回転を使用します。

  4. コードをコンパイルします。

キャラクター メッシュを設定する

  1. [Components (コンポーネント)] パネルで、Mesh Skeletal Mesh コンポーネントを選択します。

    components-mesh-select

  2. [Details] > [Mesh (メッシュ)] > [Skeletal Mesh (スケルタル メッシュ)] に移動し、ドロップダウン メニューを展開します。[Browse Asset Options (アセット オプションを参照)] > [Content (コンテンツ)] > [Show Engine Content (エンジンのコンテンツを表示)] を選択します。

    show-engine-content

  3. TutorialTPP スケルタル メッシュを検索して選択します。

    details-skeletalmesh-select

  4. [Transform (トランスフォーム)] カテゴリに移動して、メッシュの LocationRotation のベクター値を (「0.0」、「0.0」、「-90」)

    transform-details

  5. コンパイル して 保存 します。

    compile-and-save

入力コンポーネントに対するアクション/軸関数を作成する

  1. ExampleCharacter.h クラスのデフォルト内で、以下の入力関数を宣言します。

        protected:

        void MoveForward(float AxisValue);

        void MoveRight(float AxisValue); 
  1. ExampleCharacter.cpp に移動し、MoveForward メソッドと MoveRight メソッドを実装します。

        void AExampleCharacter::MoveForward(float AxisValue)
        {
            if ((Controller != NULL) && (AxisValue != 0.0f))
            {
                // find out which direction is forward
                const FRotator Rotation = Controller->GetControlRotation();
                const FRotator YawRotation(0, Rotation.Yaw, 0);

                // get forward vector
                const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
                AddMovementInput(Direction, AxisValue);
            }

        }

        void AExampleCharacter::MoveRight(float AxisValue)
        {
            if ((Controller != NULL) && (AxisValue != 0.0f))
            {
                // find out which direction is right
                const FRotator Rotation = Controller->GetControlRotation();
                const FRotator YawRotation(0, Rotation.Yaw, 0);

                // get right vector 
                const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

                // add movement in that direction
                AddMovementInput(Direction, AxisValue);
            }
        } 
  1. SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) メソッドに移動して、以下のコードを実装します。

        void AExampleCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
        {
            Super::SetupPlayerInputComponent(PlayerInputComponent);

            PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
            PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

            PlayerInputComponent->BindAxis("MoveForward", this, &AExampleCharacter::MoveForward);
            PlayerInputComponent->BindAxis("MoveRight", this, &AExampleCharacter::MoveRight);
            PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
            PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
        }
[REGION:note]
[Player Input コンポーネント](making-interactive-experiences\Input\input-overview) は、プロジェクト内の AxisMapping と ActionMapping をゲーム アクションにリンクさせます。Pawn クラスと Character クラスにはどちらも、継承したメソッドが格納されています。このメソッドはカスタム キャラクター用に使用または拡張することができます。
この例では、Pawn の AddControllerYawInput 関数と AddControllerPitchInput 関数、および Character の Jump 関数と StopJumping 関数を使用しています。
[/REGION]
  1. コードをコンパイルします。

入力イベントに対するアクション/軸関数を作成する

  1. [My Blueprint (マイ ブループリント)] > [Functions (関数)] に移動し、[Add**(**+**)] をクリックして、2 つの新しい関数を追加します。MoveForward および MoveRight** に設定します。

    move-function

  2. MoveForward 関数を選択し、[Details] > [Inputs] に移動して、[Add**(**+**)] をクリックし、「AxisValue」という名前の新しい float 値入力を追加します。

    image alt text

  3. MoveForward 関数に以下のロジックをコピーまたは実装します。

    Copy Node Graph

    move-function

  4. MoveRight 関数を選択し、手順 2 の説明に沿って、新しい float 値を追加します。

    image alt text

  5. MoveRight 関数に以下のロジックをコピーまたは実装します。

    Copy Node Graph

    move-right

  6. **イベント グラフ**に移動します。Axis 入力イベントおよび Action 入力イベントで、それぞれの関数を呼び出すように設定する必要があります。以下のロジックをコピーまたは実装します。

    Copy Node Graph

    event-graph

完成したコード

ExampleCharacter.h

        #pragma once

        #include "CoreMinimal.h"
        #include "GameFramework/Character.h"
        #include "ExampleCharacter.generated.h"

        UCLASS()
        class SETTINGUPINPUT_API AExampleCharacter : public ACharacter
        {
            GENERATED_BODY()

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

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

            UPROPERTY(EditDefaultsOnly,BlueprintReadOnly)
            class USpringArmComponent* CameraBoom;

            UPROPERTY(EditDefaultsOnly,BlueprintReadOnly)
            class UCameraComponent* FollowCamera;

              void MoveForward();

              void MoveRight();

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

            // Called to bind functionality to input
            virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

        };

ExampleCharacter.cpp

        // Sets default values
        AExampleCharacter::AExampleCharacter()
        {
            //Initialize the Camera Boom    
            CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));

            //Setup its attachment to the Root component of the class
            CameraBoom->SetupAttachment(RootComponent);

            //Set the boolean to use the PawnControlRotation to true.
            CameraBoom->bUsePawnControlRotation = true;

            //Initialize the Camera Comp
            FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));

            //Set its attachment to the Camera Boom
            FollowCamera->SetupAttachment(CameraBoom);
        }

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

        }

        void AExampleCharacter::MoveForward(float AxisValue)
        {
            if ((Controller != NULL) && (AxisValue != 0.0f))
            {
                // find out which direction is forward
                const FRotator Rotation = Controller->GetControlRotation();
                const FRotator YawRotation(0, Rotation.Yaw, 0);

                // get forward vector
                const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
                AddMovementInput(Direction, AxisValue);
            }

        }

        void AExampleCharacter::MoveRight(float AxisValue)
        {
            if ((Controller != NULL) && (AxisValue != 0.0f))
            {
                // find out which direction is right
                const FRotator Rotation = Controller->GetControlRotation();
                const FRotator YawRotation(0, Rotation.Yaw, 0);

                // get right vector 
                const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

                // add movement in that direction
                AddMovementInput(Direction, AxisValue);
            }
        }

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

        }

        // Called to bind functionality to input
        void AExampleCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
        {
        // Set up gameplay key bindings
            check(PlayerInputComponent);

        PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
            PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

            PlayerInputComponent->BindAxis("Move Forward", this, &AExampleCharacter::MoveForward);
            PlayerInputComponent->BindAxis("Move Right", this, &AExampleCharacter::MoveRight);
        } 

キャラクター ブループリントを作成する

  1. C++ Classes」フォルダに移動し、ExampleCharacter を右クリックしてドロップダウン メニューから [Create Blueprint class based on ExampleCharacter (ExampleCharacter をベースにしたブループリント クラスを作成)] を選択します。ブループリントに「BP_ExampleCharacter」という名前を付けます。

    CreateBPCharacter.png

  2. [Components (コンポーネント)] パネルで、Mesh Skeletal Mesh コンポーネントを選択します。

    components-mesh-select

  3. [Details] > [Mesh (メッシュ)] > [Skeletal Mesh (スケルタル メッシュ)] に移動し、ドロップダウン メニューを展開します。[Browser (ブラウザ)] セクションで、[Settings (設定)] アイコンをクリックします。次に、コンテキスト メニューから、[Content (コンテンツ)] > [Show Engine Content (エンジンのコンテンツを表示)] を選択します。

    show-engine-content

  4. TutorialTPP スケルタル メッシュを検索して選択します。

    details-skeletalmesh-select

  5. [Transform (トランスフォーム)] カテゴリに移動して、メッシュの LocationRotation のベクター値を (「0.0」、「0.0」、「-90」)

![transform-details](DetailsTransform.png)

完成したブループリント

MoveForward

move-function

MoveRight

move-right

イベント グラフ

event-graph

Creating the GameMode Blueprint

GameMode では、一連のゲームのルールを定義します。このルールには、ゲーム起動時にプレイヤーがどのようなデフォルト ポーンとしてスポーンされるかが含まれます。作成したプレイヤー キャラクターをスポーンするためには、これらのルールを設定する必要があります。

  1. コンテンツ ドロワー で、[Add(+)] をクリックして新しい ブループリント クラス を作成し、ドロップダウン メニューから [Game Mode Base (ゲーム モード ベース)]親クラス として選択します。ゲーム モードに「BP_InputGameMode」という名前を付けます。

    image alt text

  2. [Class Defaults (クラスのデフォルト)] で、[Classes (クラス)] > [Default Pawn Class (デフォルトの Pawn クラス)] に移動し、BP_ExampleCharacter を選択します。

    image alt text

  3. コンパイル して 保存 します。

    compile-and-save

  4. [Edit (編集)] > [Project Settings (プロジェクト設定)] > [Maps & Modes (マップ & モード)] に移動します。[Default GameMode (デフォルト ゲームモード)]BP_InputGameMode に設定します。

    image alt text

  5. エディタ に移動し、[Play (Play in Editor) (プレイ (プレイインエディタ))] を選択します。

    play-in-editor

これで、W、A、S、D キーでキャラクターの動きをコントロールできるようになりました。マウスを動かすとカメラが動き、スペース キーを押すとキャラクターがジャンプします。

GameMode では、一連のゲームのルールを定義します。これらのルールには、ゲーム起動時にプレイヤーがどのようなデフォルト ポーンとしてスポーンされるかが含まれます。作成したプレイヤー キャラクターをスポーンするためには、これらのルールを設定する必要があります。

  1. コンテンツ ドロワー で、「C++ Classes」フォルダに移動し、ドロップダウン メニューから SettingUpInputGameModeBase を右クリックし、[Create Blueprint Based on SettingUpInputGameModeBase (SettingUpInputGameModeBase をベースにしたブループリントを作成)] を選択します。ゲーム モード ブループリントに「BP_InputGameMode」という名前を付けます。

    image alt text

  2. [Class Defaults (クラスのデフォルト)] で、[Classes (クラス)] > [Default Pawn Class (デフォルトの Pawn クラス)] に移動し、BP_ExampleCharacter を選択します。

    image alt text

  3. コンパイル して 保存 します。

    compile-and-save

  4. [Edit (編集)] > [Project Settings (プロジェクト設定)] > [Maps & Modes (マップ & モード)] に移動します。[Default GameMode (デフォルト ゲームモード)]BP_InputGameMode. に設定します。

    project-settings

  5. エディタ に移動し、[Play (Play in Editor) (プレイ (プレイインエディタ))] を選択します。

    play-in-editor

これで、W、A、S、D キーでキャラクターの動きをコントロールできるようになりました。マウスを動かすとカメラが動き、スペース キーを押すとキャラクターがジャンプします。

結果

image alt text

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