首先需要添加springArmComponent以及CameraComponent,再.h文件中添加这两个组件
public:
UPROPERTY(EditAnywhere)
class USpringArmComponent* SpringArmComp;
UPROPERTY(EditAnywhere)
class UCameraComponent* CameraComp;
此时只是声明了这两个组件,之后再.cpp文件中pawn的构造函数中初始化创建这些组件同时需要一个根组件。把SpringArm放到根组件上,再把camera放到SpringArm上。
RootComponent = CreateDefaultSubobject(TEXT("RootComponent"));
SpringArmComp = CreateDefaultSubobject(TEXT("SpringArmComponent"));
CameraComp = CreateDefaultSubobject(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;
我们要通过轴映射以及操作映射对应我们的按键以及鼠标事件
然后再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(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(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(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(TEXT("RootComponent"));
SpringArmComp = CreateDefaultSubobject(TEXT("SpringArmComponent"));
CameraComp = CreateDefaultSubobject(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(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(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(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
//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
//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);
}
}
···