#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "MyPawn.generated.h"
UCLASS()
class ZWEIPROJEKT_API AMyPawn : public APawn
{
GENERATED_BODY()
private:
float XScaleIncrement = 0.0;
float YScaleIncrement = 0.0;
float Distance = 0.0;
float ZPosition = 0.0;
bool RightButtonPressed = false;
bool MiddleBtnPressed = false;
public:
// Sets default values for this pawn's properties
AMyPawn();
UPROPERTY(VisibleAnywhere)
USpringArmComponent* CameraArm;
UPROPERTY(VisibleAnywhere)
UCameraComponent* Camera;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void XAxis(float Scale);
void YAxis(float Scale);
void MiddleButtonAxis(float MiddleButtonAxis);
void RightMouseButtonPressed();
void RightMouseButtonReleased();
void MiddleButtonPressed();
void MiddleButtonReleased();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
----------------------------------------------------------------------------------------------------------------------------------------------
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Kismet/KismetMathLibrary.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;
CameraArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArm"));
RootComponent = CameraArm;
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(CameraArm);
CameraArm->bDoCollisionTest = 0;
FVector WorldOriginalCenter = FVector::ZeroVector;
SetActorLocation(WorldOriginalCenter);
Distance = CameraArm->TargetArmLength;
CameraArm->CameraRotationLagSpeed = 10.0;
CameraArm->bEnableCameraRotationLag = 1;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// 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("XAxis",this,&AMyPawn::XAxis);
PlayerInputComponent->BindAxis("YAxis", this, &AMyPawn::YAxis);
PlayerInputComponent->BindAxis("MiddleButtonAxis", this, &AMyPawn::MiddleButtonAxis);
PlayerInputComponent->BindAction("RightMouseButton", IE_Pressed,this, &AMyPawn::RightMouseButtonPressed);
PlayerInputComponent->BindAction("RightMouseButton", IE_Released, this, &AMyPawn::RightMouseButtonReleased);
PlayerInputComponent->BindAction("MiddleButtonPressed", IE_Pressed, this, &AMyPawn::MiddleButtonPressed);
PlayerInputComponent->BindAction("MiddleButtonPressed", IE_Released, this, &AMyPawn::MiddleButtonReleased);
}
void AMyPawn::XAxis(float Scale) {
if (RightButtonPressed)
{
FRotator CameraRotation = GetActorRotation();
XScaleIncrement += Scale;
FRotator ChangeRotation(CameraRotation.Pitch, XScaleIncrement, CameraRotation.Roll);
SetActorRotation(ChangeRotation);
}
}
void AMyPawn::YAxis(float Scale) {
if (RightButtonPressed)
{
FRotator CameraRotation = GetActorRotation();
YScaleIncrement -= Scale;
YScaleIncrement = UKismetMathLibrary::Clamp(YScaleIncrement ,-80,80);
FRotator ChangeRotation(YScaleIncrement, CameraRotation.Yaw, CameraRotation.Roll);
SetActorRotation(ChangeRotation);
}
if (MiddleBtnPressed)
{
FVector CameraLocation = GetActorLocation();
ZPosition = CameraLocation.Z;
ZPosition += Scale;
FVector ChangeLocation = FVector::ZeroVector;
ChangeLocation.Z = ZPosition;
SetActorLocation(ChangeLocation);
}
}
void AMyPawn::MiddleButtonAxis(float Scale) {
Distance += Scale;
CameraArm->TargetArmLength = Distance;
}
void AMyPawn::RightMouseButtonPressed() {
RightButtonPressed = true;
}
void AMyPawn::RightMouseButtonReleased() {
RightButtonPressed = false;
}
void AMyPawn::MiddleButtonPressed() {
MiddleBtnPressed = true;
}
void AMyPawn::MiddleButtonReleased() {
MiddleBtnPressed = false;
}