Character Movement ではヒューマノイド キャラクターがワールドを移動するためのロコモーション システムを提供します。Character Movement コンポーネント は Actor コンポーネント です。歩行、落下、水泳、飛行など Character クラスの動作モードを提供します。さらに、Character Movement コンポーネントはネットワーク ゲームプレイ向けの堅固な統合機能を備えており、デベロッパーがネットワークでの動作をカスタムで作成できるフレームワークを提供します。
キャラクターをアニメートするには、 Epic Games マーケットプレイス から無料でダウンロードできる Animation Starter Pack が必要です。[Free (無料)] ボタンをクリックして、Animation Starter Pack をダウンロードして、 Epic Games Library に追加します。[Free (無料)] ボタンをクリックして、アニメーション スターター パックを Epic Games ライブラリ に追加します。
ライブラリ から アニメーション スターター パック を検索して [Add to Project] をクリックします。次に [Select the Project to Add the Asset to] メニューから「CharacterMovement」ファイルを見つけて選択したら [Add to Project] をクリックします。
キャラクターをアニメートするには、Epic Games Marketplace から無料でダウンロードできる アニメーション スターター パック が必要です。[Free (無料)] ボタンをクリックして、アニメーション スターター パックを Epic Games ライブラリ に追加します。
ライブラリ から アニメーション スターター パック を検索して [Add to Project] をクリックします。次に [Select the Project to Add the Asset to] メニューから「CharacterMovement」ファイルを見つけて選択したら [Add to Project] をクリックします。
プレイヤーをコントロールするためには Character クラスが必要です。キャラクターは、ヒューマノイド キャラクターが自分のワールドを横断できるようにする移動システムを提供するための Character Movement コンポーネントを備えたポーンです。以下の手順に従って、キャラクターを設定します。
[Add/Import (追加/インポート)] ボタンをクリックして新規ブループリントの Character クラスを「BP_PlayerCharacter」という名前で作成します。
[Components (コンポーネント)] タブで、 Character Movement コンポーネント を選択して、 [Details (詳細)] パネル に移動し、 [Character Movement(Rotation Settings) (キャラクターの動き(回転設定)] カテゴリで、 Use Controller Desired Rotation および Orient Rotation To Movement をオンにします。
Character Movement:Walking カテゴリで、 Ignore Base Rotation 変数をオンにします。
Ignore Base Rotation をオンに設定すると、キャラクターは現在のワールドの回転を維持し、自分が立っているベースの回転の変化を無視します。
[Character Movement Nav Movement] カテゴリの Movement Capabilities で、ドロップダウン メニューを選択して、追加の変数設定を表示し、ブール型 Can Crouch に移動して、このチェックボックスをオンにします。
コンパイル して 保存 します。
プレイヤーをコントロールするためには Character クラスが必要です。キャラクターは、ヒューマノイド キャラクターが自分のワールドを横断できるようにする移動システムを提供するための Character Movement コンポーネントを備えたポーンです。以下の手順に従って、キャラクターを設定します。
[C++ Class Wizard (C++ クラス ウィザード)] で、新規 Character クラスを「PlayerCharacter」という名前で作成してから、「CameraCharactor.h」ファイルに移動して、次のクラス定義を宣言します。
protected:
//Spring Arm Component to follow the camera behind the player
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class USpringArmComponent* SpringArmComp;
//Player follow camera
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class UCameraComponent* CameraComp;
// Instantiating your class Components
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
//Set the location and rotation of the Character Mesh Transform
GetMesh()->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, -90.0f), FQuat(FRotator(0.0f, -90.0f, 0.0f)));
// Attaching your class Components to the default character's Skeletal Mesh Component.
SpringArmComp->SetupAttachment(GetMesh());
CameraComp->SetupAttachment(SpringArmComp,USpringArmComponent::SocketName);
//Setting class variables of the spring arm
SpringArmComp->bUsePawnControlRotation = true;
//Setting class variables of the Character movement component
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCharacterMovement()->bIgnoreBaseRotation = true;
コードを コンパイル します。
エディタ で C++ Classes フォルダ へ移動して、PlayerCharacter クラスをクリックし、C++ Class Actions ドロップダウン メニューで [Create Blueprint class based on PlayerCharacter] を選択します。ブループリントに「Bp_PlayerCharacter」と名前を付けます。
InputAxisMoveForward ノードの 実行ピン を引き出して、ドロップダウン メニュー から Add Movement Input ノードを検索して選択します。次に InputAxisMoveForward ノードの Axis Value ピンを引き出して、 Add Movement Input ノードの Scale Value ピンに接続します。
拡大表示するには、画像をクリック。
[Event Graph] で右クリックして、コンテキスト メニューで Get Control Rotation を検索して選択します。次に ローテータ型の Return Value ピンを引き出して、ドロップダウン メニューで Break Rotator を検索して選択します。
拡大表示するには、画像をクリック。
Break Rotator ノードの Z(Yaw) ピンを引き出して、ドロップダウン メニューで Make Rotator を検索して選択します。次に Make Rotator ノードの Rotator の Return Value ピンを引き出して、ドロップダウン メニューで Get Forward Vector を検索して選択します。
拡大表示するには、画像をクリック。
Get Forward Vector ノードの Vector の Return Value ピンを引き出して、 Add Movement Input ノードの World Direction ピンに接続します。
拡大表示するには、画像をクリック。
もう一度グラフで右クリックして Input Axis Event として Move Right を検索し、選択します。
拡大表示するには、画像をクリック。
InputAxis MoveRight ノードの 実行ピン を引き出して、ドロップダウン メニュー から Add Movement Input ノードを検索して選択します。次に InputAxis MoveRight ノードの Axis Value ピンを引き出して、 Add Movement Input ノードの Scale Value ピンに接続します。
拡大表示するには、画像をクリック。
Make Rotator ノードの Rotator の Return Value ピンを引き出して、ドロップダウン メニューで Get Right Vector ノードを検索し、選択します。次に Vector の Return Value ピンを引き出して Add Movement Input ノードの World Direction ピンに接続します。
拡大表示するには、画像をクリック。
完成した Movement Input Events ブループリントのグラフは以下の画像のようになります。
void APlayerCharacter::MoveForward(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way 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 APlayerCharacter::MoveRight(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way 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);
}
}
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "PlayerCharacter.generated.h"
UCLASS()
class CHARACTERMOVEMENT_API APlayerCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APlayerCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//Spring Arm Component to follow the camera behind the player
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class USpringArmComponent* SpringArmComp;
//Player follow camera
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class UCameraComponent* CameraComp;
//Called for forwards/backward input
void MoveForward(float InputAxis);
//called for left/right side input
void MoveRight(float InputAxis);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
PlayerCharacter.Cpp
#include "PlayerCharacter.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
APlayerCharacter::APlayerCharacter()
{
// 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;
// Instantiating your class Components
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
//Set the location and rotation of the Character Mesh Transform
GetMesh()->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, -90.0f), FQuat(FRotator(0.0f, -90.0f, 0.0f)));
// Attaching your class Components to the default character's Skeletal Mesh Component.
SpringArmComp->SetupAttachment(GetMesh());
CameraComp->SetupAttachment(SpringArmComp,USpringArmComponent::SocketName);
//Setting class variables of the spring arm
SpringArmComp->bUsePawnControlRotation = true;
//Setting class variables of the Character movement component
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCharacterMovement()->bIgnoreBaseRotation = true;
}
// Called when the game starts or when spawned
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();
}
void APlayerCharacter::MoveForward(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way 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 APlayerCharacter::MoveRight(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way 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 APlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward",this, &APlayerCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}
[Components (コンポーネント)] タブに移動して、 Character Movement コンポーネント をクリックして、 イベント グラフ にドラッグします。
拡大表示するには、画像をクリック。
Character Movement ピン を引き出して、ドロップダウン メニューで Set Max Walk Speed を検索して選択します。Max Walk Speed の float 値 を「1000」に設定して、 InputAction Sprint ノードの Pressed 実行ピン を Set Max Walk Speed の 入力実行 ピンに接続します。
拡大表示するには、画像をクリック。
Character Movement ピン を引き出して、もう 1 つの Set Max Walk Speed ノードを検索して選択します。Max Walk Speed の float 値 を「600」に設定して、 InputAction Sprint ノードの Released 実行ピン を Set Max Walk Speed の実行入力ピン に接続します。
protected:
//Sets Character Movement Speed to Sprint values.
void BeginSprint();
//Sets Character Movement Speed back to default speed values.
void EndSprint();
//Request for Character to Crouch
void BeginCrouch();
//Request for Character to EndCrouch
void EndCrouch();
PlayerCharacter.cpp ファイルで**APlayerCharacter::BeginSprint and APlayerCharacter::EndSprint** クラス メソッドに対して次のロジックを実装します。
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "PlayerCharacter.generated.h"
UCLASS()
class CHARACTERMOVEMENT_API APlayerCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APlayerCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//Spring Arm Component to follow the camera behind the player
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class USpringArmComponent* SpringArmComp;
//Player follow camera
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class UCameraComponent* CameraComp;
//Called for forwards/backward input
void MoveForward(float InputAxis);
//Called for left/right side input
void MoveRight(float InputAxis);
//Sets Character Movement Speed to Sprint values.
void BeginSprint();
//Sets Character Movement Speed back to default speed values.
void EndSprint();
//Request for Character to Crouch
void BeginCrouch();
//Request for Character to EndCrouch
void EndCrouch();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
PlayerCharacter.cpp
#include "PlayerCharacter.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
APlayerCharacter::APlayerCharacter()
{
// 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;
// Instantiating your class Components
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
//Set the location and rotation of the Character Mesh Transform
GetMesh()->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, -90.0f), FQuat(FRotator(0.0f, -90.0f, 0.0f)));
// Attaching your class Components to the default character's Skeletal Mesh Component.
SpringArmComp->SetupAttachment(GetMesh());
CameraComp->SetupAttachment(SpringArmComp,USpringArmComponent::SocketName);
//Setting class variables of the spring arm
SpringArmComp->bUsePawnControlRotation = true;
//Setting class variables of the Character movement component
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCharacterMovement()->bIgnoreBaseRotation = true;
}
// Called when the game starts or when spawned
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();
}
void APlayerCharacter::MoveForward(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way 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 APlayerCharacter::MoveRight(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way 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);
}
}
void APlayerCharacter::BeginSprint()
{
GetCharacterMovementComponent()->MaxWalkSpeed = 1000.0f;
}
void APlayerCharacter::EndSprint()
{
GetCharacterMovementComponent()->MaxWalkSpeed = 600.f;
}
void APlayerCharacter::BeginCrouch()
{
Crouch();
}
void APlayerCharacter::EndCrouch()
{
UnCrouch();
}
// Called every frame
void APlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward",this, &APlayerCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAction("Jump",IE_Pressed,this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump",IE_Released,this, &ACharacter::StopJumping);
PlayerInputComponent->BindAction("Crouch",IE_Pressed,this,&APlayerCharacter::BeginCrouch);
PlayerInputComponent->BindAction("Crouch",IE_Released,this,&APlayerCharacter::EndCrouch);
PlayerInputComponent->BindAction("Sprint",IE_Pressed,this, &APlayerCharacter::BeginSprint);
PlayerInputComponent->BindAction("Sprint",IE_Released,this, &APlayerCharacter::EndSprint);
}
Game Mode はゲームのルールを一通り定義します。これらのルールには、ゲームの起動時にプレイヤーがスポーンするデフォルト ポーンが含まれます。作成したプレイヤー キャラクターをスポーンするためにこれらのルールを設定する必要があります。
エディタで C++ クラス フォルダへ移動します。CharacterMovementGameModeBase を右クリックし、[C++ Class] アクション メニューのドロップダウンから、[Create Blueprint class based on your CharacterMovementGameModeBase] を選択して「Bp_GameMode」と名前を付けます。
Jump to Crouch Movement Transition rule ノードをダブルクリックして、 アニメーション グラフ を開きます。
拡大表示するには、画像をクリック。
[My Blueprint] タブで、 Is Crouched ブール変数と Is Jumping ブール変数をクリックして アニメーション グラフ にドラッグします。
拡大表示するには、画像をクリック。
Is Jumping ブール型出力ピン を引き出して、Actions ドロップダウン メニューで NOT Boolean を検索して選択し、 Is Crouched ブール値の出力ピン**を引き出して、 AND Boolean を検索して選択します。Is Jumping NOT Boolean の出力ピン を AND Boolean の入力ピン に接続して、 AND Boolean の出力ピン を Result ノードの Can Enter Transition ピン** に接続して、遷移ルールを完成させます。