UE5 C++增强输入

一.创建charactor,并且包含增强输入相关的头文件

1.项目名.build.cs。添加模块"EnhancedInput",方便找到头文件和映射的一些文件。

cpp 复制代码
	PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" ,"EnhancedInput","UMG"});

2.添加InputActionValue.h EnhancedInputComponent.h EnhancedInputSubSystems.h GameFramework/Controller.h GameFrameWork/SpringArmComponent.h Camera/CameraComponent.h

cpp 复制代码
#include "InputActionValue.h" //输入映射Value值的头文件
#include "EnhancedInputComponent.h" //增强映射的头文件
#include "EnhancedInputSubsystems.h" //增强子系统的头文件

#include "GameFramework/Controller.h" //输入控制器的头文件
#include "GameFramework/SpringArmComponent.h" //摇臂的头文件

#include "Camera/CameraComponent.h" //相机组件的头文件
#include "GameFramework/CharacterMovementComponent.h" //运动组件的头文件

3.在character头文件中,添加蓝图中的通信的变量 以及 值输入映射绑定的代理函数。

cpp 复制代码
	UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")
	USpringArmComponent* MySpringArm;
	UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")
	UCameraComponent* MyCamera;
	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category = "Input")
	class UInputMappingContext* DefaultmappingContext; //输入映射上下文
	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category = "Input")
	class UInputAction* MoveAction;  //移动映射的变量
	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category = "Input")
	class UInputAction* LookAction;  //旋转映射的变量
	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category = "MySceneComponent")
	UWidgetComponent* MyWidgetHealth;
	void Move(const FInputActionValue& Value);  //所执行的代理函数绑定
	void Look(const FInputActionValue& Value);

二.实现相关函数

1.构造函数中,构造相机。

cpp 复制代码
	PrimaryActorTick.bCanEverTick = true;
	MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArmComponent"));
	MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCameraComponent"));
	
	MySpringArm->TargetArmLength = 400.f;
	//
	MyCamera->SetupAttachment(MySpringArm); 
	MySpringArm->SetupAttachment(RootComponent);//设置跟组件。
  1. 设置控制器转动不影响角色,将角色运动面朝加速度。让pawn可以随着SpringArm旋转。
cpp 复制代码
	//让控制器的转动不影响角色的转动,只影响相机的转动
	bUseControllerRotationRoll = false;
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	GetCharacterMovement()->bOrientRotationToMovement = true; //让角色面朝加速度的方向
	MySpringArm->bUsePawnControlRotation = true;   //这个使用了Pawn控制器的旋转

3.在BeginPlay里,将Charactor自身的控制器 转换为 APlayerController,是否合法。获得本地玩家控制器的增强输入的子系统。加入 输入映射的上下文,优先级 作为 设置到子系统的映射。

cpp 复制代码
Super::BeginPlay();
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))  //如果控制器是合法的
{
	if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer())) //玩家控制器转换为 增强输入的本地玩家子系统
	{
		Subsystem->AddMappingContext(DefaultmappingContext,0);
	}
}

4.获得鼠标的输入的 2D 坐标 movementVector。判断控制器是否合法,获得控制器的旋转。将Yaw的旋转在Y轴上的 X轴上的方向的投影。移动的同时,人物先转动到相机的方向。我暂时理解的就是在控制器给定的方向移动,输入的距离。

cpp 复制代码
void AMyCharacter::Move(const FInputActionValue& Value)
{
	FVector2D movementVector = Value.Get<FVector2D>();
	if (Controller!=nullptr)
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0,Rotation.Yaw,0);
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(ForwardDirection,movementVector.Y);
		AddMovementInput(RightDirection,movementVector.X);
	}
}

5.获得鼠标的X,Y方向的移动,获得X方向上的向量值。

cpp 复制代码
void AMyCharacter::Look(const FInputActionValue& Value)
{
	FVector2D LookAxisVector = Value.Get<FVector2D>(); //获得鼠标的控制器
	if (Controller != nullptr)
	{
		AddControllerYawInput(LookAxisVector.X);
		AddControllerPitchInput(LookAxisVector.Y);
	}
}

6.玩家输入方式改为增强玩家输入映射,绑定输入(映射变量,方式,对象,绑定的函数)。

cpp 复制代码
// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent); //自带的设置
	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))  //执行输入的时候 
	{
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
		EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyCharacter::Look);
	}
	
}

7.创建继承C++的蓝图,设置Mesh。

8.设置初始化Inputmapping 上下文, 和 Action。这里用的第三人称的现成资源。

9.GameMode设置为空,测试成功。

相关推荐
ue星空1 小时前
UE5配置MRQ编解码器输出MP4视频
ue5·音视频
吴梓穆6 天前
UE5 图片9宫格切割
ue5
Kingsdesigner7 天前
游戏开发流程革命:我用Substance插件,在UE5内实现材质的实时“创世纪”
游戏·adobe·ue5·游戏引擎·游戏开发·设计师·substance 3d
幻雨様9 天前
UE5多人MOBA+GAS 37、库存系统(四)
ue5
DoomGT9 天前
Physics Simulation - UE中Projectile相关事项
ue5·游戏引擎·虚幻·虚幻引擎·unreal engine
右弦GISer11 天前
【UE5医学影像可视化】读取本地Dicom生成VolumeTexture,实现2D显示和自动翻页
ue5·dicom·医学图像
小梦白11 天前
RPG增容3:尝试使用MVC结构搭建玩家升级UI(一)
游戏·ui·ue5·mvc
AgilityBaby12 天前
解决「CPU Virtualization Technology 未开启或被占用」弹窗问题
ue5·游戏引擎·无畏契约·cpu 虚拟化技术
幻雨様13 天前
UE5多人MOBA+GAS 番外篇:同时造成多种类型伤害
ue5
幻雨様14 天前
UE5多人MOBA+GAS 番外篇:同时造成多种类型伤害,以各种属性值的百分比来应用伤害(版本二)
java·前端·ue5