unreal engine pawn controlled spring arm camera

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

#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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值