UE4 C++實現(xiàn)相機pawn(GIS操作習(xí)慣)

首先需要添加springArmComponent以及CameraComponent,再.h文件中添加這兩個組件

  public:

    UPROPERTY(EditAnywhere)
        class USpringArmComponent* SpringArmComp;

    UPROPERTY(EditAnywhere)
        class UCameraComponent* CameraComp;

此時只是聲明了這兩個組件,之后再.cpp文件中pawn的構(gòu)造函數(shù)中初始化創(chuàng)建這些組件同時需要一個根組件。把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;

我們要通過軸映射以及操作映射對應(yīng)我們的按鍵以及鼠標事件


操作映射以及軸映射.png

然后再SetupPlayerInputComponent函數(shù)中綁定這些操作

    //綁定軸事件
    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);

    //綁定鼠標狀態(tài)
    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);

綁定的函數(shù)聲明

  //按鍵平移
    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();

//旋轉(zhuǎn)
    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函數(shù)中使他平滑過渡
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實現(xiàn),其中UKismetMathLibrary是需要加載的數(shù)學(xué)庫,#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));
    }

旋轉(zhuǎn)方法

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

平移方法,右鍵平移,可以用官網(wǎng)的那種方法,可以用平滑,我這邊用的是射線法更像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)
{
    //實現(xiàn)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);
    }
    
}

全部代碼,里面有寫初始化以及其他內(nèi)容,上面只是講了思路
.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;//旋轉(zhuǎn)速度

    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;//是否可以旋轉(zhuǎn)

    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);

    //綁定鼠標狀態(tài)
    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)
{
    //實現(xiàn)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向前軸,旋轉(zhuǎn)pawn后平移有問題
···
void APawnBase::MoveForward(float AxisValue)
{
//實現(xiàn)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);
}

}
···

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Unreal Engine 4 Scripting with C++ Cookbook 簡單筆記 主動刪除Obj...
    鏡月s閱讀 9,413評論 0 1
  • C++暴露給藍圖可編輯 UCLASS(Blueprintable) 創(chuàng)建FString FString::Prin...
    Lif68閱讀 833評論 0 0
  • 學(xué)習(xí)利用組件將Pawn與物理交互、使用粒子效果等方法。 創(chuàng)建 Pawn 子類 為了能對 Actor 進行控制, 創(chuàng)...
    wjundong閱讀 136評論 0 0
  • 玩家輸入與Pawn 這個時組件和碰撞的提前知識預(yù)備,最好在之前了解組件相關(guān)東西,重要的概念對于軸映射和操作映射。 ...
    貧僧這就來超度施主閱讀 457評論 0 0
  • 類型 int8/ uint8 :8位有符號/無符號整數(shù) int16/ uint16 :16位有符號/無符號整數(shù) i...
    右腕閱讀 1,114評論 0 1

友情鏈接更多精彩內(nèi)容