#loc UE4DocJpn File updated against INT#2672917

[CL 2682249 by Masayo Kondo in Main branch]
This commit is contained in:
Masayo Kondo
2015-09-07 04:56:47 -04:00
committed by Masayo.Kondo@epicgamesjapan.com
parent e7459c33b0
commit abab7ec22e

View File

@@ -1,4 +1,4 @@
INTSourceChangelist:2664711
INTSourceChangelist:2672917
Availability:Docs
Title:2.7 - カメラビューの変更
Crumbs: %ROOT%, Programming, Programming/Tutorials/FirstPersonShooter, Programming/Tutorials/FirstPersonShooter/2
@@ -24,6 +24,228 @@ Description:First Person Shooter キャラクターのカメラビューの変
%Steps%
<!--TODO:Add tutorial steps here-->
前回の手順の最後で、デフォルトのカメラはメッシュのネックの内側に位置付けられました。このステップでは、カメラのプロパティ (位置やFOV (視野角) など) を調整するために使用可能な適切な FPS カメラを使用してセットアップします。
## Camera コンポーネントをアタッチする
1. Visual Studio に切り替えて、`FPSCharacter.h` を開き、以下のパブリック プロパティを追加します。
// FPS カメラ
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
UCameraComponent* FirstPersonCameraComponent;
1. 以下のコンストラクタを `FPSCharacter.h` に追加します。
// AFPSCharacter のコンストラクタ
AFPSCharacter(const FObjectInitializer& ObjectInitializer);
1. `FPSCharacter.h` は以下のようになります。
// Project Settings の Description ページに著作権情報を入力してください。
#pragma once
#include "GameFramework/Character.h"
#include "FPSCharacter.generated.h"
UCLASS()
class FPSPROJECT_API AFPSCharacter : public ACharacter
{
GENERATED_BODY()
public:
// このキャラクターのプロパティのデフォルト値を設定
AFPSCharacter();
// AFPSCharacter のコンストラクタ
AFPSCharacter(const FObjectInitializer& ObjectInitializer);
// ゲーム開始時またはスポーン時に呼ばれます。
virtual void BeginPlay() override;
// フレームごとに呼ばれます。
virtual void Tick( float DeltaSeconds ) override;
// 機能と入力をバインドするために呼ばれます。
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
// 前後方向の移動を処理します。
UFUNCTION()
void MoveForward(float Value);
// 左右の移動を処理します。
UFUNCTION()
void MoveRight(float Value);
// キー押下時のジャンプフラグを設定
UFUNCTION()
void OnStartJump();
//キー解放時のジャンプフラグをクリア
UFUNCTION()
void OnStopJump();
// FPS カメラ
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
UCameraComponent* FirstPersonCameraComponent;
};
1. `FPSCharacter.cpp` を開いて次のコードを追加します。
// camera コンポーネントを作成するコンストラクタ
AFPSCharacter::AFPSCharacter(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{
// Camera コンポーネントを作成
FirstPersonCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
if (FirstPersonCameraComponent != NULL)
{
FirstPersonCameraComponent->AttachParent = GetCapsuleComponent();
}
}
[REGION:note]
前述のコードは、`CameraComponent` を作成し、それを `CapsuleComponent` にアタッチします。
[/REGION]
1. 次のコードを `AFPSCharacter::AFPSCharacter(const FObjectInitializer& ObjectInitializer)` にアタッチします。
// 目の高さより少し上にカメラを設定
FirstPersonCameraComponent->RelativeLocation = FVector(0.f, 0.f, 50.f + BaseEyeHeight);
// ポーンが回転を制御できるようにします。
FirstPersonCameraComponent->bUsePawnControlRotation = true;
[REGION:note]
前述のコードは、カメラの位置をキャラクターの目の位置より少し高めに調整します。
[/REGION]
1. `FPSCharacter.cpp` は以下のようになります。
// Project Settings の Description ページに著作権情報を入力してください。
#include "FPSProject.h"
#include "FPSCharacter.h"
// デフォルト値を設定
AFPSCharacter::AFPSCharacter()
{
// このキャラクターがフレーム毎に Tick() を呼び出すように設定します。必要がなければパフォーマンスを向上させるためにオフにすることができます。
PrimaryActorTick.bCanEverTick = true;
}
// camera コンポーネントを作成するコンストラクタ
AFPSCharacter::AFPSCharacter(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{
// Camera コンポーネントを作成
FirstPersonCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
if (FirstPersonCameraComponent != NULL)
{
FirstPersonCameraComponent->AttachParent = GetCapsuleComponent();
// 目の高さより少し上にカメラを設定
FirstPersonCameraComponent->RelativeLocation = FVector(0.f, 0.f, 50.f + BaseEyeHeight);
// ポーンが回転を制御できるようにします。
FirstPersonCameraComponent->bUsePawnControlRotation = true;
}
}
// ゲーム開始時またはスポーン時に呼ばれます。
void AFPSCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine != nullptr)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("We are using FPSCharacter."));
}
}
// フレームごとに呼ばれます。
void AFPSCharacter::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// 機能と入力をバインドするために呼ばれます。
void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
// ゲームプレイのキーバインドをセットアップ
InputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
// ゲームプレイのマウス バインドのセットアップ
InputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
InputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
// ジャンプ アクションのバインドのセットアップ。
InputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::OnStartJump);
InputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::OnStopJump);
}
//前方向の移動の関数の定義
void AFPSCharacter::MoveForward(float Value)
{
if (Controller != nullptr && Value != 0.f)
{
// 前進方向の確認
FRotator Rotation = Controller->GetControlRotation();
// 歩行または落下時のピッチ (上下動) の制限
if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling())
{
Rotation.Pitch = 0.f;
}
// その方向へ動きを追加
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
//右移動の関数の定義
void AFPSCharacter::MoveRight(float Value)
{
if (Controller != nullptr && Value != 0.f)
{
// 右方向の確認
const FRotator Rotation = Controller->GetControlRotation();
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
// その方向へ動きを追加
AddMovementInput(Direction, Value);
}
}
//ジャンプ フラグを設定する関数
void AFPSCharacter::OnStartJump()
{
bPressedJump = true;
}
//ジャンプ フラグをクリアする関数
void AFPSCharacter::OnStopJump()
{
bPressedJump = false;
}
## 新しいカメラをテストする
新規で実装したカメラのコードをコンパイルし、テストします。
1. Visual Studio に `FPSCharacter` interface (\*.h) と implementation (\*.cpp) ファイルを保存します。
1. **[Solution Explorer (ソリューション エクスプローラ)]** で **[FPSProject]** を探します。
1. **[FPSProject]** 上で **右クリック** して、**[Build (ビルド)]** を選択してプロジェクトをコンパイルします。
![](BuildProject.png)
1. ビルドが終了したらアンリアル エディタでご自身の **FPSProject** を開きます。
1. レベル エディタのツールバーで、**[Play (再生)]** ボタンを **クリック** します。カメラはキャラクター頭部より上に位置していなくてはいけません。下を向くと、キャラクターの頭部が見えるはずです。
![](NewCameraComponent.png)
1. [PIE (Play In Editor)] モードを終了するには、レベル エディタで **[Stop]** ボタンを **クリック** します。
%Steps%