UE4 C++实现相机pawn(GIS操作习惯)

首先需要添加springArmComponent以及CameraComponent,再.h文件中添加这两个组件

  public:

    UPROPERTY(EditAnywhere)
        class USpringArmComponent* SpringArmComp;

    UPROPERTY(EditAnywhere)
        class UCameraComponent* CameraComp;

此时只是声明了这两个组件,之后再.cpp文件中pawn的构造函数中初始化创建这些组件同时需要一个根组件。把SpringArm放到根组件上,再把camera放到SpringArm上。

    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
    CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));

    //绑定组件
    SpringArmComp->SetupAttachment(RootComponent);
    CameraComp->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);

    // 为SpringArm类的变量赋值
    SpringArmComp->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 0.0f), FRotator(-30.0f, 0.0f, 0.0f));
    SpringArmComp->TargetArmLength = 400.f;
    SpringArmComp->bEnableCameraLag = false;
    SpringArmComp->bEnableCameraRotationLag = true;
    SpringArmComp->CameraRotationLagSpeed = 15.f;
    SpringArmComp->CameraLagSpeed = 3.0f;

我们要通过轴映射以及操作映射对应我们的按键以及鼠标事件


操作映射以及轴映射.png

然后再SetupPlayerInputComponent函数中绑定这些操作

    //绑定轴事件
    InputComponent->BindAxis("MoveForward", this, &ATPawn::MoveForward);
    InputComponent->BindAxis("MoveRight", this, &ATPawn::MoveRight);
    InputComponent->BindAxis("MoveUp", this, &ATPawn::MoveUp);

    InputComponent->BindAxis("LookUpRate", this, &ATPawn::LookUpRate);
    InputComponent->BindAxis("TurnRate", this, &ATPawn::TurnRate);

    //绑定鼠标状态
    InputComponent->BindAction("OnClickLeftMouseButton", IE_Pressed, this, &ATPawn::OnPressedLeftMouseButton);
    InputComponent->BindAction("OnClickLeftMouseButton", IE_Released, this, &ATPawn::OnRealeasedLeftMouseButton);

    InputComponent->BindAction("OnClickRightMouseButton", IE_Pressed, this, &ATPawn::OnPressedRightMouseButton);
    InputComponent->BindAction("OnClickRightMouseButton", IE_Released, this, &ATPawn::OnRealeasedRightMouseButton);


    InputComponent->BindAxis("Zoom", this, &ATPawn::Zoom);

绑定的函数声明

  //按键平移
    void MoveForward(float AxisValue);
    void MoveRight(float AxisValue);
    void MoveUp(float AxisValue);
//鼠标点击
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnPressedLeftMouseButton();
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnRealeasedLeftMouseButton();
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnPressedRightMouseButton();
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnRealeasedRightMouseButton();

//旋转
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void LookUpRate(float AxisValue);
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void TurnRate(float AxisValue);
//缩放
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void Zoom(float AxisValue);

缩放:需要有个平滑的效果,先算出滚轮的下一次长度,然后再在tick函数中使他平滑过渡
zoom方法

void ATPawn::Zoom(float AxisValue)
{
    if (AxisValue > 0 && !bIsMoving && !bIsRotating && bIsZooming)
    {
        if(bZoomCanIn(-AxisValue*ZoomSpeed,SpringArmComp)){
            NextFrameArmLength = this->SpringArmComp->TargetArmLength / 1.5;
        }
    }
    else if (AxisValue < 0 && !bIsMoving && !bIsRotating && bIsZooming) {
        if (bZoomCanOut(-AxisValue * ZoomSpeed, SpringArmComp)) {
            NextFrameArmLength = this->SpringArmComp->TargetArmLength*1.5;
        }
    }
}

tick实现,其中UKismetMathLibrary是需要加载的数学库,#include "Kismet/KismetMathLibrary.h"

if (!bIsRotating&&!bIsMoving)
    {
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength,MinSprArmLength,MaxSprArmLength);
        SpringArmComp->TargetArmLength = UKismetMathLibrary::FInterpTo(SpringArmComp->TargetArmLength, NextFrameArmLength,GetWorld()->GetDeltaSeconds(),UKismetMathLibrary::Lerp(10.0f,5.0f,springArmLengthNormalized));
    }

旋转方法

void ATPawn::LookUpRate(float AxisValue)
{
    CameraInput.Y = AxisValue;
    if (bIsRotating)
    {
        RotateSpringArm(UKismetMathLibrary::MakeRotator(0, AxisValue * this->RotateSpeed, 0), this->SpringArmComp);
        if (bIsMoving)
        {
            //bisRightMouseButtonDown = false;
            bIsMoving = false;
        }
    }
}

void ATPawn::TurnRate(float AxisValue)
{
    CameraInput.X = AxisValue;
    if (bIsRotating)
    {
        RotateSpringArm(UKismetMathLibrary::MakeRotator(0, 0, AxisValue * this->RotateSpeed), this->SpringArmComp);
        if (bIsMoving)
        {
            //bisRightMouseButtonDown = false;
            bIsMoving = false;
        }
    }
}
void ATPawn::RotateSpringArm(FRotator InRotatorOffset, USpringArmComponent* TargetSprArm)
{
    if (TargetSprArm)
    {
        TargetSprArm->AddRelativeRotation(LimitRotationOffset(InRotatorOffset, this->SpringArmComp, this->MaxSprArmPitchValue, this->MinSprArmPitchValue) );
    }
}
//限制镜头
FRotator ATPawn::LimitRotationOffset(FRotator InRotator, USpringArmComponent* TargetSpringArm, float MaxPitch, float MinPitch)
{
    float NextFramePitchValue = TargetSpringArm->GetRelativeRotation().Pitch + InRotator.Pitch;
    if (NextFramePitchValue >= MaxPitch || NextFramePitchValue <= MinPitch)
    {
        return UKismetMathLibrary::MakeRotator(InRotator.Roll, 0.0f, InRotator.Yaw);
    }
    else
    {
        return InRotator;
    }
}

平移方法,右键平移,可以用官网的那种方法,可以用平滑,我这边用的是射线法更像gis软件中右键指定到哪里就是哪里

void ATPawn::OnPressedLeftMouseButton()
{
    bIsLeftMouseButtonDown = true;

    bIsMoving = false;
    bIsRotating = true;
}

void ATPawn::OnRealeasedLeftMouseButton()
{
    bIsLeftMouseButtonDown = false;

    bIsRotating = false;
    bIsMoving = false;
}

void ATPawn::OnPressedRightMouseButton()
{
    bIsRightMouseButtonDown = true;
    SpringArmComp->bEnableCameraLag = false;
    //点击时获取点击的位置,把位置初始化
    FVector MouseLocation, MouseDircetion, LineTraceEnd;
    FHitResult  hitResult(ForceInit);
    FCollisionQueryParams ColQuerryPara(FName(TEXT("Combattrace")), true, NULL);
    ColQuerryPara.bTraceComplex = false;
    ColQuerryPara.bReturnPhysicalMaterial = false;

    PawnPlayerController->DeprojectMousePositionToWorld(MouseLocation, MouseDircetion);
    LineTraceEnd = MouseLocation + (MouseDircetion * LineTarceLength);

    GetWorld()->LineTraceSingleByChannel(hitResult, MouseLocation, LineTraceEnd, ECC_GameTraceChannel2, ColQuerryPara);
    //GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + hitResult.Location.ToString());
    StartVector = hitResult.Location;
    


    bIsRotating = false;
    bIsMoving = true;
}

void ATPawn::OnRealeasedRightMouseButton()
{
    bIsRightMouseButtonDown = false;
    bIsRotating = false;
    bIsMoving = false;
}

tick

if (bIsMoving && !bIsRotating)
    {
        FVector MouseLocation, MouseDircetion, LineTraceEnd;
        FHitResult  hitResult(ForceInit);
        FCollisionQueryParams ColQuerryPara(FName(TEXT("Combattrace")), true, NULL);
        ColQuerryPara.bTraceComplex = false;
        ColQuerryPara.bReturnPhysicalMaterial = false;

        ColQuerryPara.AddIgnoredActor(this);//绕过自身
        PawnPlayerController->DeprojectMousePositionToWorld(MouseLocation, MouseDircetion);
        LineTraceEnd = MouseLocation + (MouseDircetion * LineTarceLength);

        bool bIsHit = GetWorld()->LineTraceSingleByChannel(hitResult, MouseLocation, LineTraceEnd, ECC_GameTraceChannel2, ColQuerryPara);

        //判断是否移动非常小的距离如果小于就不进行操作,不然会一直抖动
        if((hitResult.Location - StartVector).Size()>= MoveDistanceTolerance){
            FVector TargetMoveDir;
            TargetMoveDir = UKismetMathLibrary::GetDirectionUnitVector(hitResult.Location , StartVector);
            PawnDeltaLocation = (hitResult.Location - StartVector).Size() * TargetMoveDir;
            if (bIsHit)
            {
                MovePawn(this, PawnDeltaLocation);
            }
        }
    }

按键平移就可以平滑类似官方的方法

void ATPawn::MoveForward(float AxisValue)
{
    //实现float的clamp(利用模板类)
    MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    //GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + MovementInput.ToString());
    if (MovementInput.X != 0.0f) {
        SpringArmComp->bEnableCameraLag = true;
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);
        PawnDeltaLocation = FVector(MovementInput.X * 500.0f * springArmLengthNormalized, 0,0);
        //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
        MovePawn(this, PawnDeltaLocation);
    }
    
}

void ATPawn::MoveRight(float AxisValue)
{
    MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    //GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + MovementInput.ToString());
    if (MovementInput.Y != 0.0f) {
        SpringArmComp->bEnableCameraLag = true;
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);
        PawnDeltaLocation = FVector(0,MovementInput.Y * 500.0f * springArmLengthNormalized, 0);
        //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
        MovePawn(this, PawnDeltaLocation);
    }
}

void ATPawn::MoveUp(float AxisValue)
{
    float moveUp = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    if(moveUp != 0.0f){
        SpringArmComp->bEnableCameraLag = true;
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);
        PawnDeltaLocation = FVector(0, 0, moveUp*500.0f* springArmLengthNormalized);
        //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
        MovePawn(this, PawnDeltaLocation);
    }
    
}

全部代码,里面有写初始化以及其他内容,上面只是讲了思路
.h文件

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "TPawn.generated.h"



USTRUCT(BlueprintType)
struct FS_CameraAnimParams
{
    GENERATED_USTRUCT_BODY()

public:
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        float Pitch;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        float Yaw;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        float SpringArmLength;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FVector Location;
};

UCLASS()
class ECHARTS_API ATPawn : public APawn
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    ATPawn();

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;

public:
    UPROPERTY(EditAnywhere)
        class USpringArmComponent* SpringArmComp;

    UPROPERTY(EditAnywhere)
        class UCameraComponent* CameraComp;
public:

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float MinSprArmPitchValue;//最大的俯仰角

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float MaxSprArmPitchValue;//最小的俯仰角度

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float RotateSpeed;//旋转速度

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float MoveSpeed;//移动速度

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float MaxMoveSpeed;//最大的移动速度

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float ZoomSpeed;//缩放速度

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float MinSprArmLength;//最大的摇臂长度

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float MaxSprArmLength;//最小的摇臂长度

    UPROPERTY(VisibleAnywhere, Category = "MouseState")
        bool bIsMoving;//是否可以移动

    UPROPERTY(VisibleAnywhere, Category = "MouseState")
        bool bIsRotating;//是否可以旋转

    UPROPERTY(VisibleAnywhere, Category = "MouseState")
        bool bIsZooming;//是否可以缩放

    UPROPERTY(VisibleAnywhere, Category = "MouseState")
        bool bIsLeftMouseButtonDown;//是否按下了左键
    UPROPERTY(VisibleAnywhere, Category = "MouseState")
        bool bIsRightMouseButtonDown;//是否按下了右键

    UPROPERTY(VisibleAnywhere, Category = "MouseState")
        class APlayerController* PawnPlayerController;


    UPROPERTY(VisibleAnywhere, Category = "BaseConfig")
        float NextFrameArmLength;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float LineTarceLength;


    UPROPERTY(VisibleAnywhere, Category = "BaseConfig")
        FVector StartVector;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "BaseConfig")
        float MoveDistanceTolerance;

    UPROPERTY(VisibleAnywhere, Category = "Move")
        FVector PawnDeltaLocation;
public:
    void MoveForward(float AxisValue);
    void MoveRight(float AxisValue);
    void MoveUp(float AxisValue);

    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnPressedLeftMouseButton();
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnRealeasedLeftMouseButton();
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnPressedRightMouseButton();
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void OnRealeasedRightMouseButton();


    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void LookUpRate(float AxisValue);
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void TurnRate(float AxisValue);
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void Zoom(float AxisValue);
    UFUNCTION(BlueprintCallable, Category = "MouseEvents")
        void RotateSpringArm(FRotator InRotatorOffset, USpringArmComponent* TargetSprArm);
    UFUNCTION(Category = "Move")
        bool MovePawn(APawn* TargetPawn, FVector Location);


    bool bZoomCanIn(float DeltaOffset, USpringArmComponent* TargetSprArm);
    bool bZoomCanOut(float DeltaOffset, USpringArmComponent* TargetSprArm);


    FVector2D MovementInput;
    FVector2D CameraInput;
    bool bZoomingIn;


public:
    UFUNCTION(BlueprintPure, Category = "InputMode")
        FRotator LimitRotationOffset(FRotator InRotator, USpringArmComponent* TargetSpringArm, float MaxPitch, float MinPitch);

};

.cpp文件

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


#include "TPawn.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Camera/CameraComponent.h"

#include "Kismet/GameplayStatics.h"

// Sets default values
ATPawn::ATPawn()
{
    // 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;

    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
    CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));

    //绑定组件
    SpringArmComp->SetupAttachment(RootComponent);
    CameraComp->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
    // 为SpringArm类的变量赋值
    SpringArmComp->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 0.0f), FRotator(-30.0f, 0.0f, 0.0f));
    SpringArmComp->TargetArmLength = 400.f;
    SpringArmComp->bEnableCameraLag = false;
    SpringArmComp->bEnableCameraRotationLag = true;
    SpringArmComp->CameraRotationLagSpeed = 15.f;
    SpringArmComp->CameraLagSpeed = 3.0f;

    NextFrameArmLength = SpringArmComp->TargetArmLength;

    //初始化变量
    bIsZooming = true;
    bIsMoving = false;
    bIsRotating = false;

    MinSprArmPitchValue = -89.0f;
    MaxSprArmPitchValue = 5.0f;
    RotateSpeed = 2.5f;

    MoveSpeed = 30.0f;
    MaxMoveSpeed = 1500.0f;

    ZoomSpeed = 10.0f;
    MinSprArmLength = 10.0f;
    MaxSprArmLength = 15000.0f;

    LineTarceLength = 100000000.0f;
    MoveDistanceTolerance = 0.5f;
}

// Called when the game starts or when spawned
void ATPawn::BeginPlay()
{
    Super::BeginPlay();
    
    PawnPlayerController = UGameplayStatics::GetPlayerController(this, 0);

    StartVector = ATPawn::GetActorLocation();
}

// Called every frame
void ATPawn::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    if (bIsMoving && !bIsRotating)
    {
        FVector MouseLocation, MouseDircetion, LineTraceEnd;
        FHitResult  hitResult(ForceInit);
        FCollisionQueryParams ColQuerryPara(FName(TEXT("Combattrace")), true, NULL);
        ColQuerryPara.bTraceComplex = false;
        ColQuerryPara.bReturnPhysicalMaterial = false;

        ColQuerryPara.AddIgnoredActor(this);//绕过自身
        PawnPlayerController->DeprojectMousePositionToWorld(MouseLocation, MouseDircetion);
        LineTraceEnd = MouseLocation + (MouseDircetion * LineTarceLength);

        bool bIsHit = GetWorld()->LineTraceSingleByChannel(hitResult, MouseLocation, LineTraceEnd, ECC_GameTraceChannel2, ColQuerryPara);

        //判断是否移动非常小的距离如果小于就不进行操作,不然会一直抖动
        if((hitResult.Location - StartVector).Size()>= MoveDistanceTolerance){
            FVector TargetMoveDir;
            TargetMoveDir = UKismetMathLibrary::GetDirectionUnitVector(hitResult.Location , StartVector);
            PawnDeltaLocation = (hitResult.Location - StartVector).Size() * TargetMoveDir;
            if (bIsHit)
            {
                MovePawn(this, PawnDeltaLocation);
            }
        }
    }else if (!bIsRotating&&!bIsMoving)
    {
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength,MinSprArmLength,MaxSprArmLength);
        SpringArmComp->TargetArmLength = UKismetMathLibrary::FInterpTo(SpringArmComp->TargetArmLength, NextFrameArmLength,GetWorld()->GetDeltaSeconds(),UKismetMathLibrary::Lerp(10.0f,5.0f,springArmLengthNormalized));
    }

}

// Called to bind functionality to input
void ATPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    //绑定轴事件
    InputComponent->BindAxis("MoveForward", this, &ATPawn::MoveForward);
    InputComponent->BindAxis("MoveRight", this, &ATPawn::MoveRight);
    InputComponent->BindAxis("MoveUp", this, &ATPawn::MoveUp);

    InputComponent->BindAxis("LookUpRate", this, &ATPawn::LookUpRate);
    InputComponent->BindAxis("TurnRate", this, &ATPawn::TurnRate);

    //绑定鼠标状态
    InputComponent->BindAction("OnClickLeftMouseButton", IE_Pressed, this, &ATPawn::OnPressedLeftMouseButton);
    InputComponent->BindAction("OnClickLeftMouseButton", IE_Released, this, &ATPawn::OnRealeasedLeftMouseButton);

    InputComponent->BindAction("OnClickRightMouseButton", IE_Pressed, this, &ATPawn::OnPressedRightMouseButton);
    InputComponent->BindAction("OnClickRightMouseButton", IE_Released, this, &ATPawn::OnRealeasedRightMouseButton);


    InputComponent->BindAxis("Zoom", this, &ATPawn::Zoom);
}

void ATPawn::MoveForward(float AxisValue)
{
    //实现float的clamp(利用模板类)
    MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    //GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + MovementInput.ToString());
    if (MovementInput.X != 0.0f) {
        SpringArmComp->bEnableCameraLag = true;
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);
        PawnDeltaLocation = FVector(MovementInput.X * 500.0f * springArmLengthNormalized, 0,0);
        //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
        MovePawn(this, PawnDeltaLocation);
    }
    
}

void ATPawn::MoveRight(float AxisValue)
{
    MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    //GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + MovementInput.ToString());
    if (MovementInput.Y != 0.0f) {
        SpringArmComp->bEnableCameraLag = true;
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);
        PawnDeltaLocation = FVector(0,MovementInput.Y * 500.0f * springArmLengthNormalized, 0);
        //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
        MovePawn(this, PawnDeltaLocation);
    }
}

void ATPawn::MoveUp(float AxisValue)
{
    float moveUp = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
    if(moveUp != 0.0f){
        SpringArmComp->bEnableCameraLag = true;
        float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);
        PawnDeltaLocation = FVector(0, 0, moveUp*500.0f* springArmLengthNormalized);
        //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
        MovePawn(this, PawnDeltaLocation);
    }
    
}

void ATPawn::OnPressedLeftMouseButton()
{
    bIsLeftMouseButtonDown = true;

    bIsMoving = false;
    bIsRotating = true;
}

void ATPawn::OnRealeasedLeftMouseButton()
{
    bIsLeftMouseButtonDown = false;

    bIsRotating = false;
    bIsMoving = false;
}

void ATPawn::OnPressedRightMouseButton()
{
    bIsRightMouseButtonDown = true;
    SpringArmComp->bEnableCameraLag = false;
    //点击时获取点击的位置,把位置初始化
    FVector MouseLocation, MouseDircetion, LineTraceEnd;
    FHitResult  hitResult(ForceInit);
    FCollisionQueryParams ColQuerryPara(FName(TEXT("Combattrace")), true, NULL);
    ColQuerryPara.bTraceComplex = false;
    ColQuerryPara.bReturnPhysicalMaterial = false;

    PawnPlayerController->DeprojectMousePositionToWorld(MouseLocation, MouseDircetion);
    LineTraceEnd = MouseLocation + (MouseDircetion * LineTarceLength);

    GetWorld()->LineTraceSingleByChannel(hitResult, MouseLocation, LineTraceEnd, ECC_GameTraceChannel2, ColQuerryPara);
    //GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + hitResult.Location.ToString());
    StartVector = hitResult.Location;
    


    bIsRotating = false;
    bIsMoving = true;
}

void ATPawn::OnRealeasedRightMouseButton()
{
    bIsRightMouseButtonDown = false;
    bIsRotating = false;
    bIsMoving = false;
}

void ATPawn::LookUpRate(float AxisValue)
{
    CameraInput.Y = AxisValue;
    if (bIsRotating)
    {
        RotateSpringArm(UKismetMathLibrary::MakeRotator(0, AxisValue * this->RotateSpeed, 0), this->SpringArmComp);
        if (bIsMoving)
        {
            //bisRightMouseButtonDown = false;
            bIsMoving = false;
        }
    }
}

void ATPawn::TurnRate(float AxisValue)
{
    CameraInput.X = AxisValue;
    if (bIsRotating)
    {
        RotateSpringArm(UKismetMathLibrary::MakeRotator(0, 0, AxisValue * this->RotateSpeed), this->SpringArmComp);
        if (bIsMoving)
        {
            //bisRightMouseButtonDown = false;
            bIsMoving = false;
        }
    }
}

void ATPawn::Zoom(float AxisValue)
{
    if (AxisValue > 0 && !bIsMoving && !bIsRotating && bIsZooming)
    {
        if(bZoomCanIn(-AxisValue*ZoomSpeed,SpringArmComp)){
            NextFrameArmLength = this->SpringArmComp->TargetArmLength / 1.5;
            //this->ZoomSpeed = SpringArmComp->TargetArmLength / 20.0f;
            //NextFrameArmLength = this->SpringArmComp->TargetArmLength - AxisValue * ZoomSpeed;
            //SpringArmComp->TargetArmLength = NextFrameArmLength;//修改摇臂长度
        }
    }
    else if (AxisValue < 0 && !bIsMoving && !bIsRotating && bIsZooming) {
        if (bZoomCanOut(-AxisValue * ZoomSpeed, SpringArmComp)) {
            NextFrameArmLength = this->SpringArmComp->TargetArmLength*1.5;
            //this->ZoomSpeed = SpringArmComp->TargetArmLength / 20.0f;
            //NextFrameArmLength = this->SpringArmComp->TargetArmLength - AxisValue * ZoomSpeed;
            //SpringArmComp->TargetArmLength = NextFrameArmLength;//修改摇臂长度
        }
    }
}

bool ATPawn::bZoomCanIn(float DeltaOffset, USpringArmComponent* TargetSprArm)
{
    if (TargetSprArm)
    {
        float NextFrameSprArmLength = DeltaOffset + TargetSprArm->TargetArmLength;
        if (NextFrameSprArmLength >= MinSprArmLength)
        {
            return true;
        }
        else {
            return false;
        }
    }
    else
    {
        return false;
    }
}

bool ATPawn::bZoomCanOut(float DeltaOffset, USpringArmComponent* TargetSprArm)
{
    if (TargetSprArm)
    {
        float NextFrameSprArmLength = DeltaOffset + TargetSprArm->TargetArmLength;
        if (NextFrameSprArmLength <= MaxSprArmLength)
        {
            return true;
        }
        else {
            return false;
        }
    }
    else
    {
        return false;
    }
}

void ATPawn::RotateSpringArm(FRotator InRotatorOffset, USpringArmComponent* TargetSprArm)
{
    if (TargetSprArm)
    {
        TargetSprArm->AddRelativeRotation(LimitRotationOffset(InRotatorOffset, this->SpringArmComp, this->MaxSprArmPitchValue, this->MinSprArmPitchValue) );
    }
}

bool ATPawn::MovePawn(APawn* TargetPawn, FVector Location)
{
    if (TargetPawn)
    {
        TargetPawn->AddActorWorldOffset(Location);
        return true;
    }
    return false;
}

FRotator ATPawn::LimitRotationOffset(FRotator InRotator, USpringArmComponent* TargetSpringArm, float MaxPitch, float MinPitch)
{
    float NextFramePitchValue = TargetSpringArm->GetRelativeRotation().Pitch + InRotator.Pitch;
    if (NextFramePitchValue >= MaxPitch || NextFramePitchValue <= MinPitch)
    {
        return UKismetMathLibrary::MakeRotator(InRotator.Roll, 0.0f, InRotator.Yaw);
    }
    else
    {
        return InRotator;
    }
}


更新,wsad操作没有按照pawn向前轴,旋转pawn后平移有问题
···
void APawnBase::MoveForward(float AxisValue)
{
//实现float的clamp(利用模板类)
MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
//GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + MovementInput.ToString());
if (MovementInput.X != 0.0f) {
SpringArmComp->bEnableCameraLag = true;
float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);
//获取向前轴
FVector ForwardVector = UKismetMathLibrary::GetForwardVector(SpringArmComp->GetRelativeRotation());
FVector PlaneNormal = UKismetMathLibrary::ProjectVectorOnToPlane(ForwardVector,FVector(0,0,1));
//GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Green, ForwardVector.ToString());

    PawnDeltaLocation = FVector(PlaneNormal.X*(MovementInput.X * 500.0f * springArmLengthNormalized), PlaneNormal.Y * (MovementInput.X * 500.0f * springArmLengthNormalized), 0);
    //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
    MovePawn(this, PawnDeltaLocation);
}

}

void APawnBase::MoveRight(float AxisValue)
{
MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
//GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Orange, "" + MovementInput.ToString());
if (MovementInput.Y != 0.0f) {
SpringArmComp->bEnableCameraLag = true;
float springArmLengthNormalized = UKismetMathLibrary::NormalizeToRange(SpringArmComp->TargetArmLength, MinSprArmLength, MaxSprArmLength);

    FVector ForwardVector = UKismetMathLibrary::GetRightVector(SpringArmComp->GetRelativeRotation());
    FVector PlaneNormal = UKismetMathLibrary::ProjectVectorOnToPlane(ForwardVector, FVector(0, 0, 1));

    PawnDeltaLocation = FVector(PlaneNormal.X * (MovementInput.Y * 500.0f * springArmLengthNormalized), PlaneNormal.Y * (MovementInput.Y * 500.0f * springArmLengthNormalized), 0);
    //GEngine->AddOnScreenDebugMessage(-1, 33.5f, FColor::Orange, "" + PawnDeltaLocation.ToString());
    MovePawn(this, PawnDeltaLocation);
}

}
···

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容

  • Unreal Engine 4 Scripting with C++ Cookbook 简单笔记 主动删除Obj...
    镜月s阅读 8,742评论 0 1
  • C++暴露给蓝图可编辑 UCLASS(Blueprintable) 创建FString FString::Prin...
    Lif68阅读 571评论 0 0
  • 学习利用组件将Pawn与物理交互、使用粒子效果等方法。 创建 Pawn 子类 为了能对 Actor 进行控制, 创...
    wjundong阅读 98评论 0 0
  • 玩家输入与Pawn 这个时组件和碰撞的提前知识预备,最好在之前了解组件相关东西,重要的概念对于轴映射和操作映射。 ...
    贫僧这就来超度施主阅读 374评论 0 0
  • 类型 int8/ uint8 :8位有符号/无符号整数 int16/ uint16 :16位有符号/无符号整数 i...
    右腕阅读 1,009评论 0 1