Playerの作成
キャラクタの操作
ACppPlayerController.cpp/.hを修正してキャラクタを移動させます。
ACppPlayerController.hを変更する
移動用のメソッドvoid PadMove(void)
と
操作対象のポインタACppPlyBase* MyActor;
を加えます。
それに伴いクラスの前方参照宣言class ACppPlyBase;
を追加します。
ACppPlayerController.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;
class ACppPlyBase;
//========================================================================
/// プレイヤーコントローラー
//========================================================================
UCLASS()
class ACppPlayerController : public APlayerController
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
public:
ACppPlayerController();
virtual void Tick(float DeltaTime) override;
protected:
void PadMove(void);
protected:
ACppPlyBase* MyActor; //!< 操作対象のActor
UArmCamera *ArmCamera = nullptr; //!< アームカメラ
IvPadInput PadInput; //!< Key入力
};
ACppPlayerController.cppを変更する
インクルードヘッダを追加する
以下のヘッダを追加する
- CppPlyBase.h : 操作するキャラクタのクラス
- GameFramework/CharacterMovementComponent.h :移動させるためのコンポーネント
抜粋:ACppPlayerController.cpp
#include "CppPlyBase.h"
#include "GameFramework/CharacterMovementComponent.h"
操作対象のアクターを取得する
BeginPlay()
で操作対象のアクタ(ACppPlyBase)を取得します。
void ACppPlayerController::BeginPlay() {
Super::BeginPlay();
//操作対象のアクターを取得する
MyActor = dynamic_cast <ACppPlyBase*> (GetCharacter());
if (!MyActor) return;
// カメラをセットアップする
ArmCamera = UArmCamera::Create(MyActor);
FVector ofs(0, 0, 100);//注視点を1m高くする
ArmCamera->SetRelativeLocation(ofs);
}
移動の実装1
'ACppPlayerController::PadMove(void)'の実装です。
GamePadの左ジョイステック、またはW,A,S,Dの入力でキャラクタを移動させます。
void ACppPlayerController::PadMove(void) {
float speedRate = PadInput.JoyLdist;
if (PadInput.JoyLdist) {
// カメラアームの 方向 を取得
const FRotator Rotation = ArmCamera->GetRelativeRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// PADの 方向 を取得
float angl = atan2(PadInput.JoyL_Y, -PadInput.JoyL_X);
// 移動方向を計算
angl += FMath::DegreesToRadians(-90 + Rotation.Yaw);
UPawnMovementComponent* MovementComponent = MyActor->GetMovementComponent();
if (MovementComponent->Velocity.Z != 0) {
angl = atan2(MovementComponent->Velocity.Y, MovementComponent->Velocity.X);
}
FVector vec(0, 0, 0);
vec.X = cos(angl);
vec.Y = sin(angl);
MyActor->AddMovementInput(vec, speedRate);
}
}
移動の実装2
PadMove(void)を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);
}
//移動
PadMove();
}
これで実行します。
.
ACppPlayerController.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 "CppPlyBase.h"
#include "../../../uty/ArmCamera.h"
#include "GameFramework/CharacterMovementComponent.h"
//----------------------------------------------------------------
/// コンストラクタ
//----------------------------------------------------------------
ACppPlayerController::ACppPlayerController() {
PrimaryActorTick.bCanEverTick = true; //Tickを呼ばれるようにする
}
//----------------------------------------------------------------
/// BeginPlay()
//----------------------------------------------------------------
void ACppPlayerController::BeginPlay() {
Super::BeginPlay();
MyActor = dynamic_cast <ACppPlyBase*> (GetCharacter());
if (!MyActor) return;
// カメラをセットアップする
ArmCamera = UArmCamera::Create(MyActor);
FVector ofs(0, 0, 100);//注視点を1m高くする
ArmCamera->SetRelativeLocation(ofs);
}
//----------------------------------------------------------------
///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);
}
//移動
PadMove();
}
//----------------------------------------------------------------
///PAD入力による移動
//----------------------------------------------------------------
void ACppPlayerController::PadMove(void) {
float speedRate = PadInput.JoyLdist;
if (PadInput.JoyLdist) {
// カメラアームの 方向 を取得
const FRotator Rotation = ArmCamera->GetRelativeRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// PADの 方向 を取得
float angl = atan2(PadInput.JoyL_Y, -PadInput.JoyL_X);
// 移動方向を計算
angl += FMath::DegreesToRadians(-90 + Rotation.Yaw);
UPawnMovementComponent* MovementComponent = MyActor->GetMovementComponent();
if (MovementComponent->Velocity.Z != 0) {
angl = atan2(MovementComponent->Velocity.Y, MovementComponent->Velocity.X);
}
FVector vec(0, 0, 0);
vec.X = cos(angl);
vec.Y = sin(angl);
MyActor->AddMovementInput(vec, speedRate);
}
}
移動方向を向かせる
BeginPlay()に以下のコードを追加する
auto cmove = MyActor->GetCharacterMovement();
cmove->bOrientRotationToMovement = true;
cmove->RotationRate = FRotator(0.0f, 360.0f * 3, 0.0f);
RotationRateは
UseControllerDesiredRotationまたはOrientRotationToMovementが
trueの場合に使用される1秒あたりの回転数の変化。
無限回転数と瞬時回転数には負の値を設定してください。
公式: RotationRate について
BeginPlay()
void ACppPlayerController::BeginPlay() {
Super::BeginPlay();
MyActor = dynamic_cast <ACppPlyBase*> (GetCharacter());
if (!MyActor) return;
// カメラをセットアップする
ArmCamera = UArmCamera::Create(MyActor);
FVector ofs(0, 0, 100);//注視点を1m高くする
ArmCamera->SetRelativeLocation(ofs);
//移動方向を向かせる
{
MyActor->bUseControllerRotationPitch = false;
MyActor->bUseControllerRotationYaw = false;
MyActor->bUseControllerRotationRoll = false;
auto cmove = MyActor->GetCharacterMovement();
cmove->bOrientRotationToMovement = true;
cmove->RotationRate = FRotator(0.0f, 360.0f * 3, 0.0f);
}
}