gamemode.h
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyGameModeBase.h"
#include "MyPawn.h"
#include "MyPlayerController.h"
AMyGameModeBase::AMyGameModeBase()
{
//AGameModeBase = AMyGameModeBase::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass();
DefaultPawnClass = AMyPawn::StaticClass();
}
gamemode.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MyGameModeBase.generated.h"
/**
*
*/
UCLASS()
class CPP511SELF_API AMyGameModeBase : public AGameModeBase
{
GENERATED_BODY()
public:
AMyGameModeBase();
};
pawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class CPP511SELF_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
UPROPERTY(VisibleAnywhere)
class USpringArmComponent* SpringArm;
class UCameraComponent* camera;
class APlayerController* playercontroller;
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;
private:
float armlength;
void lookup(float value);
void lookright(float value);
void lookzoom(float zoom);
};
pawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AMyPawn::AMyPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
AutoPossessPlayer = EAutoReceiveInput::Player0;
RootComponent = CreateAbstractDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
SpringArm = CreateAbstractDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
camera = CreateAbstractDefaultSubobject<UCameraComponent>(TEXT("camera"));
SpringArm->SetupAttachment(GetRootComponent());
camera->SetupAttachment(SpringArm);
armlength = 500.0f;
SpringArm->TargetArmLength = armlength;
SpringArm->bDoCollisionTest = false;
SpringArm->bUsePawnControlRotation = true;
//playercontroller = UGameplayStatics::GetPlayerController(GetWorld(), 0);
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
APlayerController* playercontrol = UGameplayStatics::GetPlayerController(GetWorld(), 0);
playercontrol->bShowMouseCursor = true;
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("lookup"), this, &AMyPawn::lookup);
PlayerInputComponent->BindAxis(TEXT("lookright"), this, &AMyPawn::lookright);
PlayerInputComponent->BindAxis(TEXT("lookzoom"), this, &AMyPawn::lookzoom);
}
void AMyPawn::lookup(float value)
{
APlayerController* playercontrol = UGameplayStatics::GetPlayerController(GetWorld(), 0);
if (playercontrol->IsInputKeyDown(EKeys::LeftMouseButton))
{
AddControllerPitchInput(value);
}
}
void AMyPawn::lookright(float value)
{
APlayerController* playercontrol = UGameplayStatics::GetPlayerController(GetWorld(), 0);
if (playercontrol->IsInputKeyDown(EKeys::LeftMouseButton))
{
AddControllerYawInput(value);
}
}
void AMyPawn::lookzoom(float zoom)
{
SpringArm->TargetArmLength = FMath::Clamp((SpringArm->TargetArmLength + 1 * zoom), 100.0f, 600.0f);
}