// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Pawn.h"#include"MyCharacter.generated.h"UCLASS()classCITYTWIN_API AMyCharacter :public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesAMyCharacter();//box作为根组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUBoxComponent* CollisionBox;//相机组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUCameraComponent* FollowCamera;//相机臂组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUSpringArmComponent* CameraBoom;protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay()override;public:// Called every framevirtualvoidTick(float DeltaTime)override;// Called to bind functionality to inputvirtualvoidSetupPlayerInputComponent(classUInputComponent* PlayerInputComponent)override;};
声明创建组件,硬编码一些基本属性
// Fill out your copyright notice in the Description page of Project Settings.#include"MyCharacter.h"#include"Components/BoxComponent.h"#include"Camera/CameraComponent.h"#include"GameFramework/SpringArmComponent.h"// Sets default valuesAMyCharacter::AMyCharacter(){// 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;CollisionBox =CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));CollisionBox->SetupAttachment(GetRootComponent());CameraBoom =CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(CollisionBox);CameraBoom->TargetArmLength =1500.f;CameraBoom->bUsePawnControlRotation =false;CameraBoom->bEnableCameraLag =true;//摄像机臂平滑CameraBoom->bEnableCameraRotationLag =true;//启用相机旋转延迟CameraBoom->bDoCollisionTest =false;//关闭摄像机臂碰撞FollowCamera =CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom);FollowCamera->bUsePawnControlRotation =false;}// Called when the game starts or when spawnedvoidAMyCharacter::BeginPlay(){Super::BeginPlay();}// Called every framevoidAMyCharacter::Tick(float DeltaTime){Super::Tick(DeltaTime);}// Called to bind functionality to inputvoidAMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){Super::SetupPlayerInputComponent(PlayerInputComponent);}
创建这个Pawn类的蓝图进行之前登录页面动画的视角旋转方向问题的解决
首先将蓝图生成对齐到视角
将Pawn类自动接收玩家0
然后删除PlayerStart
运行结果
移动增强输入系统
在项目建立.cs文件中添加这个模块
绑定增强输入系统操作
MyCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Pawn.h"#include"../../../../UE_5.2.1/UE_5.2/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/InputActionValue.h"#include"MyCharacter.generated.h"UCLASS()classCITYTWIN_API AMyCharacter :public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesAMyCharacter();//box作为根组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUBoxComponent* CollisionBox;//相机组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUCameraComponent* FollowCamera;//相机臂组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUSpringArmComponent* CameraBoom;//绑定映射UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input")classUInputMappingContext* MappingContext;//移动绑定UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input")classUInputAction* LeftButtonAction;protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay()override;//鼠标按下操作事件voidLeftMouseDown(const FInputActionValue& value);voidLeftMouseUp(const FInputActionValue& value);public:// Called every framevirtualvoidTick(float DeltaTime)override;// Called to bind functionality to inputvirtualvoidSetupPlayerInputComponent(classUInputComponent* PlayerInputComponent)override;};
MyCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"MyCharacter.h"#include"Components/BoxComponent.h"#include"Camera/CameraComponent.h"#include"GameFramework/SpringArmComponent.h"#include"EnhancedInputSubsystems.h"#include"EnhancedInputComponent.h"// Sets default valuesAMyCharacter::AMyCharacter(){// 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;CollisionBox =CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));CollisionBox->SetupAttachment(GetRootComponent());CameraBoom =CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(CollisionBox);CameraBoom->TargetArmLength =1500.f;CameraBoom->bUsePawnControlRotation =false;CameraBoom->bEnableCameraLag =true;//摄像机臂平滑CameraBoom->bEnableCameraRotationLag =true;//启用相机旋转延迟CameraBoom->bDoCollisionTest =false;//关闭摄像机臂碰撞FollowCamera =CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom);FollowCamera->bUsePawnControlRotation =false;}// Called when the game starts or when spawnedvoidAMyCharacter::BeginPlay(){Super::BeginPlay();APlayerController* PlayerController =Cast<APlayerController>(Controller);if(PlayerController){UEnhancedInputLocalPlayerSubsystem* Subsystem =ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());if(Subsystem){Subsystem->AddMappingContext(MappingContext,0);}}}voidAMyCharacter::LeftMouseDown(const FInputActionValue& value){}voidAMyCharacter::LeftMouseUp(const FInputActionValue& value){}// Called every framevoidAMyCharacter::Tick(float DeltaTime){Super::Tick(DeltaTime);}// Called to bind functionality to inputvoidAMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){Super::SetupPlayerInputComponent(PlayerInputComponent);UEnhancedInputComponent* EnhancedInputConponent =Cast<UEnhancedInputComponent>(PlayerInputComponent);if(EnhancedInputConponent){EnhancedInputConponent->BindAction(LeftButtonAction, ETriggerEvent::Started,this,&AMyCharacter::LeftMouseDown);EnhancedInputConponent->BindAction(LeftButtonAction, ETriggerEvent::Completed,this,&AMyCharacter::LeftMouseUp);}}
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Pawn.h"#include"../../../../UE_5.2.1/UE_5.2/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/InputActionValue.h"#include"MyCharacter.generated.h"UCLASS()classCITYTWIN_API AMyCharacter :public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesAMyCharacter();//box作为根组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUBoxComponent* CollisionBox;//相机组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUCameraComponent* FollowCamera;//相机臂组件UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Camera")classUSpringArmComponent* CameraBoom;//绑定映射UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input")classUInputMappingContext* MappingContext;//移动绑定UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input")classUInputAction* LeftButtonAction;//移动绑定UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input")classUInputAction* RightButtonAction;//移动绑定UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input")classUInputAction* MiddleButtonAction;//鼠标的坐标点FVector2D MousePos = FVector2D::ZeroVector;//记录的鼠标变化差量FVector2D MouseVariationDifference = FVector2D::ZeroVector;//记录变化之后的鼠标坐标float MouseX =0.f;float MouseY =0.f;//鼠标视角移动速度float MouseSpeed =0.1f;//鼠标右键移动速度float MouseRightSpeed =100.f;//鼠标中键移动速度float MouseMiddleSpeed =1000.f;//标识鼠标左键是否按下bool bIsMouseLeftDown =false;//标识鼠标右键是否按下bool bIsMouseRightDown =false;protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay()override;//鼠标按下操作事件voidLeftMouseDown(const FInputActionValue& value);voidLeftMouseUp(const FInputActionValue& value);voidRightMouseDown(const FInputActionValue& value);voidRightMouseUp(const FInputActionValue& value);voidMiddleMouseSlide(const FInputActionValue& value);//鼠标移动视角移动事件voidMouseMove();public:// Called every framevirtualvoidTick(float DeltaTime)override;// Called to bind functionality to inputvirtualvoidSetupPlayerInputComponent(classUInputComponent* PlayerInputComponent)override;voidGetMousePosition();};
MyCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"MyCharacter.h"#include"Components/BoxComponent.h"#include"Camera/CameraComponent.h"#include"GameFramework/SpringArmComponent.h"#include"EnhancedInputSubsystems.h"#include"EnhancedInputComponent.h"#include"Engine/Engine.h"// Sets default valuesAMyCharacter::AMyCharacter(){// 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;CollisionBox =CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));CollisionBox->SetupAttachment(GetRootComponent());CameraBoom =CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(CollisionBox);CameraBoom->TargetArmLength =1500.f;CameraBoom->bUsePawnControlRotation =false;CameraBoom->bEnableCameraLag =true;//摄像机臂平滑CameraBoom->bEnableCameraRotationLag =true;//启用相机旋转延迟CameraBoom->bDoCollisionTest =false;//关闭摄像机臂碰撞FollowCamera =CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom);FollowCamera->bUsePawnControlRotation =false;}// Called when the game starts or when spawnedvoidAMyCharacter::BeginPlay(){Super::BeginPlay();APlayerController* PlayerController =Cast<APlayerController>(Controller);if(PlayerController){UEnhancedInputLocalPlayerSubsystem* Subsystem =ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());if(Subsystem){Subsystem->AddMappingContext(MappingContext,0);}}}voidAMyCharacter::LeftMouseDown(const FInputActionValue& value){bIsMouseLeftDown =true;}voidAMyCharacter::LeftMouseUp(const FInputActionValue& value){bIsMouseLeftDown =false;}voidAMyCharacter::RightMouseDown(const FInputActionValue& value){bIsMouseRightDown =true;}voidAMyCharacter::RightMouseUp(const FInputActionValue& value){bIsMouseRightDown =false;}voidAMyCharacter::MiddleMouseSlide(const FInputActionValue& value){float Axis = value.Get<float>();float CameraArm = CameraBoom->TargetArmLength;//限制相机臂长度CameraBoom->TargetArmLength =FMath::Clamp((CameraArm - Axis * MouseMiddleSpeed),1000.f,30000.f);}voidAMyCharacter::MouseMove(){if(bIsMouseLeftDown){FRotator ActorRotator =GetActorRotation();//限制一下视角度数带来的错误FRotator NewActorRotator =FRotator(FMath::Clamp((ActorRotator.Pitch +(MouseVariationDifference.Y * MouseSpeed)),-80.f,-10.f),ActorRotator.Yaw +(MouseVariationDifference.X * MouseSpeed *-1.f), ActorRotator.Roll);SetActorRotation(NewActorRotator);}if(bIsMouseRightDown){FVector SpeedMove =FVector(MouseVariationDifference.Y * MouseRightSpeed,-1.0* MouseRightSpeed * MouseVariationDifference.X,0);AddActorLocalOffset(SpeedMove);//限制z轴FVector Location =GetActorLocation();FVector NewLocation =FVector(Location.X, Location.Y,4520);SetActorLocation(NewLocation);}}// Called every framevoidAMyCharacter::Tick(float DeltaTime){Super::Tick(DeltaTime);//获取鼠标坐标GetMousePosition();//鼠标移动视角MouseMove();}// Called to bind functionality to inputvoidAMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){Super::SetupPlayerInputComponent(PlayerInputComponent);UEnhancedInputComponent* EnhancedInputConponent =Cast<UEnhancedInputComponent>(PlayerInputComponent);if(EnhancedInputConponent){EnhancedInputConponent->BindAction(LeftButtonAction, ETriggerEvent::Started,this,&AMyCharacter::LeftMouseDown);EnhancedInputConponent->BindAction(LeftButtonAction, ETriggerEvent::Completed,this,&AMyCharacter::LeftMouseUp);EnhancedInputConponent->BindAction(RightButtonAction, ETriggerEvent::Started,this,&AMyCharacter::RightMouseDown);EnhancedInputConponent->BindAction(RightButtonAction, ETriggerEvent::Completed,this,&AMyCharacter::RightMouseUp);EnhancedInputConponent->BindAction(MiddleButtonAction, ETriggerEvent::Triggered,this,&AMyCharacter::MiddleMouseSlide);}}voidAMyCharacter::GetMousePosition(){//获取当前鼠标移动坐标GetWorld()->GetFirstPlayerController()->GetMousePosition(MousePos.X, MousePos.Y);//记录的鼠标变化差量MouseVariationDifference = MousePos -FVector2D(MouseX, MouseY);//记录变化后的鼠标位置MouseX = MousePos.X;MouseY = MousePos.Y;//打印鼠标信息到屏幕/*GEngine->AddOnScreenDebugMessage(1, 1, FColor::Red, FString::Printf(TEXT("%f,%f"), MousePos.X, MousePos.Y));GEngine->AddOnScreenDebugMessage(2, 1, FColor::Yellow, FString::Printf(TEXT("%f,%f"), MouseVariationDifference.X, MouseVariationDifference.Y));*/}
59. 螺旋矩阵II ★★ 力扣题目链接,给你一个正整数 n ,生成一个包含 1 到 n 2 n^2 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。1 < n < 20
示例 1: 输入:n 3
输出:[[1,…