LoginSignup
4
0

More than 3 years have passed since last update.

最初に

この記事はC++側からサードパーソンにおけるカメラの回転方法を記事にしたものとなります。間違いや更に良い方法があった場合にはそっとTwitter( @ろっさむ )やコメント、修正リクエストなどでお知らせ頂けるととても有難いです。

作業環境

記事内で使用する作業環境は以下の通りとなります。

  • Unreal Engine:4.23.1
  • Visual Studio:Community 2017 or Community 2017 for Mac or Enterprise 2019

実装方法

【UE4】コントローラー入力に対するセットアップをC++で行う にて記載した、C++側からInputのキーをMappingして登録する手法とよく似ています。

まずはプレイヤーキャラクターのクラスのヘッダーファイルにてYawPitchそれぞれのマウス感度を表す定数とYaw(float amount)Pitch(float amount)を定義します。

// CPP_PlayerCharacter.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Characters/Base/CPP_CharacterBase.h"
#include "CPP_PlayerCharacter.generated.h"

/**
 * 
 */
UCLASS()
class ACPP_PlayerCharacter : public ACPP_CharacterBase
{
    GENERATED_BODY()

private:

    static constexpr float YawAmount = 1.0f;
    static constexpr float PitchAmount = -1.0f;
    static constexpr float MouseSensitivity = 100.0f;

    // Inputに処理を割り当てるためのセットアップメソッド
    virtual void  SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;

    void Yaw(const float amount);
    void Pitch(const float amount);    
};

次にcppファイル側を変更します。

// CPP_PlayerCharacter.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "CPP_PlayerCharacter.h"

#include "Components/InputComponent.h"
#include "GameFramework/PlayerInput.h"
#include "GameFramework/PlayerController.h"
#include "Engine/World.h"
#include "GameFramework/CharacterMovementComponent.h"

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

    check(PlayerInputComponent);

    // MouseのX座標更新でのイベント登録するための情報群
    FInputAxisKeyMapping Yaw("Yaw", EKeys::MouseX, YawAmount);

    // MouseのY座標更新でのイベント登録するための情報群
    FInputAxisKeyMapping Pitch("Pitch", EKeys::MouseY, PitchAmount);

    // 実際に登録を行う
    GetWorld()->GetFirstPlayerController()->PlayerInput->AddAxisMapping(Yaw);
    GetWorld()->GetFirstPlayerController()->PlayerInput->AddAxisMapping(Pitch);

    // Yaw()とPitch()をバインドして実際に反応するようにする
    PlayerInputComponent->BindAxis("Yaw",  this, &ACPP_PlayerCharacter::Yaw);
    PlayerInputComponent->BindAxis("Pitch", this, &ACPP_PlayerCharacter::Pitch);

}

void ACPP_PlayerCharacter::Yaw(const float amount)
{
    auto YawValue = MouseSensitivity * amount * GetWorld()->GetDeltaSeconds();
    AddControllerYawInput(YawValue);
}

void ACPP_PlayerCharacter::Pitch(const float amount)
{
    auto PitchValue = MouseSensitivity * amount * GetWorld()->GetDeltaSeconds();
    AddControllerPitchInput(PitchValue);
}

4
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0