xahgo
(xahgo)
October 5, 2018, 5:50am
1
As shown in the short source code below, a character was created and the SpringArm was connected and moved.
However, if you move using AddMovementInput, the camera does not follow. What’s the matter with you?
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Soldier1.generated.h"
UCLASS()
class ACTIONCODE_API ASoldier1 : public ACharacter {
GENERATED_BODY()
public: // Sets default values for this character's properties
ASoldier1();
protected: // Called when the game starts or when spawned
virtual void BeginPlay() override;
public: // Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
void MoveForward( float AxisValue );
void MoveRight( float AxisValue );
void PitchCamera( float AxisValue );
void YawCamera( float AxisValue );
void ZoomIn();
void ZoomOut();
FVector2D CameraInput;
float ZoomFactor;
bool bZoomingIn;
USpringArmComponent* springArm;
UCameraComponent* camera;
};
=======================================================================================
Soldier.cpp
#include "Soldier1.h"
#include "Kismet/GameplayStatics.h"
#include <assert.h>
ASoldier1::ASoldier1()
{
PrimaryActorTick.bCanEverTick = true;
RootComponent = CreateDefaultSubobject<USceneComponent>( TEXT( "RootComponent" ) );
springArm = CreateDefaultSubobject<USpringArmComponent>( TEXT( "CameraSpringArm" ) );
springArm->SetupAttachment( RootComponent );
springArm->SetRelativeLocationAndRotation( FVector( 0.0f, 0.0f, 50.0f ), FRotator( -60.0f, 0.0f, 0.0f ) );
springArm->TargetArmLength = 400.f;
springArm->bEnableCameraLag = true;
springArm->CameraLagSpeed = 3.0f;
camera = CreateDefaultSubobject<UCameraComponent>( TEXT( "GameCamera" ) );
camera->SetupAttachment( springArm, USpringArmComponent::SocketName );
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
void ASoldier1::BeginPlay()
{
Super::BeginPlay();
}
void ASoldier1::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if( springArm ) {
FRotator newYaw = GetActorRotation();
FRotator newPitch = springArm->GetComponentRotation();
newYaw.Yaw += CameraInput.X;
newPitch.Pitch += CameraInput.Y;
SetActorRotation( newYaw );
springArm->SetWorldRotation( newPitch );
if( bZoomingIn ) {
ZoomFactor += DeltaTime / 0.5f; //0.5 초에 걸쳐 줌인
} else {
ZoomFactor -= DeltaTime / 0.25f; //0.25 초에 걸쳐 줌아웃
}
ZoomFactor = FMath::Clamp<float>( ZoomFactor, 0.0f, 1.0f );
camera->FieldOfView = FMath::Lerp<float>( 90.0f, 60.0f, ZoomFactor );
springArm->TargetArmLength = FMath::Lerp<float>( 400.0f, 300.0f, ZoomFactor );
}
}
void ASoldier1::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAction( "ZoomIn", IE_Pressed, this, &ASoldier1::ZoomIn );
InputComponent->BindAction( "ZoomIn", IE_Released, this, &ASoldier1::ZoomOut );
InputComponent->BindAxis( "MoveForward", this, &ASoldier1::MoveForward );
InputComponent->BindAxis( "MouseYaw", this, &ASoldier1::YawCamera );
InputComponent->BindAxis( "MousePitch", this, &ASoldier1::PitchCamera );
}
//입력 함수
void ASoldier1::MoveForward( float AxisValue )
{
//MovementInput.X = FMath::Clamp<float>( AxisValue, -1.0f, 1.0f );
FVector vWorldDir = camera->GetForwardVector();
AddMovementInput( vWorldDir * AxisValue );
}
void ASoldier1::MoveRight( float AxisValue )
{
MovementInput.Y = FMath::Clamp<float>( AxisValue, -1.0f, 1.0f );
}
void ASoldier1::PitchCamera( float AxisValue )
{
CameraInput.Y = AxisValue;
}
void ASoldier1::YawCamera( float AxisValue )
{
CameraInput.X = AxisValue;
}
void ASoldier1::ZoomIn()
{
bZoomingIn = true;
}
void ASoldier1::ZoomOut()
{
bZoomingIn = false;
}