Playerの作成
カメラの操作
カメラは、IvPadInput.h
を使って操作します。
参照: UE4/C++ キーとGamePad入力を取得する
IvPadInputはCppPlayerControllerにインスタンスします。
CppPlayerController.hの変更
CppPlayerController.h内にIvPadInput.h
をインクルードします。
続いてクラス内にPadInput
をインスタンスします。
IvPadInput PadInput; //!< Key入力
CppPlayerController.h
/**
* @file CppPlayerController.h
* @brief プレイヤーコントローラー
* @author inuvatico
* @date 2020/10/17
* @version 1.0
* @copyright 2020 inuvatico
* Released under the MIT license.
* see https://opensource.org/licenses/MIT
* @par (new/Add/Change : 2020/10/17)
*
*/
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "../../Uty/IvPadInput.h" //Key入力の取得
#include "CppPlayerController.generated.h"
//クラスの前方宣言
class UArmCamera;
//========================================================================
/// プレイヤーコントローラー
//========================================================================
UCLASS()
class ACppPlayerController : public APlayerController
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
public:
ACppPlayerController();
virtual void Tick(float DeltaTime) override;
protected:
UArmCamera *ArmCamera = nullptr; //!< アームカメラ
IvPadInput PadInput; //!< Key入力
};
CppPlayerController.cppの変更
カメラ操作はTick()で行います。 カメラ操作の前にはKey入力の更新が必要です。
CppPlayerController.cpp抜粋
void ACppPlayerController::Tick(float DeltaTime) {
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 1.0f / 560.f, FColor::Orange, FString::Printf(TEXT("Tick")));
PadInput.update(this); //Key入力の更新
//カメラ の更新
if (PadInput.JoyRdist) {
ArmCamera->AddRotation(PadInput.JoyR_X, PadInput.JoyR_Y);
}
}
CppPlayerController.cpp全文
/**
* @file CppPlayerController.cpp
* @brief プレイヤーコントローラー
* @author inuvatico
* @date 2020/10/17
* @version 1.0
* @copyright 2020 inuvatico
* Released under the MIT license.
* see https://opensource.org/licenses/MIT
* @par (new/Add/Change : 2020/10/17)
*
*/
#include "CppPlayerController.h"
#include "ArmCamera.h"
//----------------------------------------------------------------
/// コンストラクタ
//----------------------------------------------------------------
ACppPlayerController::ACppPlayerController() {
PrimaryActorTick.bCanEverTick = true; //Tickを呼ばれるようにする
}
//----------------------------------------------------------------
/// BeginPlay()
//----------------------------------------------------------------
void ACppPlayerController::BeginPlay() {
Super::BeginPlay();
AActor *TrgActor = (AActor *)GetCharacter();//プレイヤーキャラクタを取得
if (!TrgActor) return;
// カメラをセットアップする
ArmCamera = UArmCamera::Create((AActor *)TrgActor);
}
//----------------------------------------------------------------
///Tick
//----------------------------------------------------------------
void ACppPlayerController::Tick(float DeltaTime) {
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 1.0f / 560.f, FColor::Orange, FString::Printf(TEXT("Tick")));
PadInput.update(this); //Key入力の更新
//カメラ の更新
if (PadInput.JoyRdist) {
ArmCamera->AddRotation(PadInput.JoyR_X, PadInput.JoyR_Y);
}
}
実行する
プレイを押して実行します。
↑↓←→Keyを押してカメラが動けば成功です。
GamePadの右ジョイスティックでも動きます。