虚幻引擎5:增强输入的使用方法

一、包引入

在项目中的build.cs文件中引入包

二、基本配置

1.创建一个输入映射上下文(映射表)

2.创建自己需要的操作映射或者轴映射

3.创建完成之后进入这个映射,来设置类型,共有4个类型

1.Digital:是旧版操作映射类型,一般是按下抬起来使用,像跳跃,之类的

2.剩余三个都是轴映射类型,Axis1D:单向轴,一般是油门按键,摇杆之类的

3.Axis2D:有x,y两个方向,2D轴,一般是移动之类的

4.Axis3D:有x,y,z三个方向

4.将设置的映射添加至上下文中

1.操作映射的配置就添加个按键直接使用就好了
2.轴映射的配置

5.然后设置对应的按键。基本设置就完成了

三、使用方法

然后我这边选择使用C++来使用

1.在使用此配置的角色类中引入按键映射mapping

cpp 复制代码
//头文件代码
UPROPERTY(EditAnywhere)//此标记可以在蓝图中导入映射资产
class UInputMappingContext*MappingContext;
UPROPERTY(EditAnywhere)
class UInputAction * JumpAction;//跳跃事件的action
UPROPERTY(EditAnywhere)
UInputAction * MoveAction;//移动事件的action
virtual void BeginPlay() override;
virtual void ALGPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
//轴映射移动绑定的函数,必须带有一下参数格式必须一致
void Move(const FInputActionValue& Value);

//cpp文件代码
//按键操作一般在beginplay中执行
void ALGPlayerCharacter::BeginPlay()
{
	Super::BeginPlay();
    //判断一下资产是否添加,否则UE会崩溃
	if(MappingContext)
	{
        //因为按键是在角色身上执行的,所以需要找到角色控制器
		if (APlayerController* PC = Cast<APlayerController>(GetController()))
		{
            //通过角色控制找到增强输入子系统
UEnhancedInputLocalPlayerSubsystem*Subsystem=ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer());
            //将映射表添加至系统中
			Subsystem->AddMappingContext(MappingContext,0);
		}
		
	}
}

void ALGPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
//使用了增强输入后,旧的输入控件就无法使用,必须使用新的
	if(UEnhancedInputComponent*EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
	{
        //跳跃事件的绑定
		EnhancedInputComponent->BindAction(JumpAction,ETriggerEvent::Started,this,&ALGPlayerCharacter::DoJump);
        //移动事件的绑定
		EnhancedInputComponent->BindAction(MoveAction,ETriggerEvent::Triggered,this,&ALGPlayerCharacter::Move);
	}
}

//绑定的移动事件
void ALGPlayerCharacter::Move(const FInputActionValue& Value)
{
    获取绑定事件给出的轴值
	FVector2D InputValue = Value.Get<FVector2D>();
	UE_LOG(LogTemp, Log, TEXT("ok%s"),*InputValue.ToString())
    //前后移动
	AddMovementInput(GetActorForwardVector(), InputValue.Y);
    //左右移动
	AddMovementInput(GetActorRightVector(), InputValue.X);
}
相关推荐
zhangzhangkeji18 小时前
UE5 蓝图-游老师-21-22-组件:组件构成了角色 actor
ue5
zhangzhangkeji1 天前
UE5 材质-25-各种节点:点乘dot,VertexNormalWS 节点与 CameraVectorWS 节点,
ue5
zhangzhangkeji1 天前
UE5 材质-23:材质里参数的分组与排序。材质参数集,为了批量修改很多个材质实例里的参数的值。
ue5
zhangzhangkeji1 天前
UE5 材质-24-各种节点:叉积cross,调试节点 DebugFloat3Values,拆分向量 SplitComponents,
ue5
zhangzhangkeji2 天前
UE5 蓝图-游老师-23-射线检测物体与碰撞规则设置:按通道检测与按类型检测;以及修改项目设置(引擎-碰撞)以自定义碰撞类型
ue5
成都渲染101云渲染66662 天前
C4D 云渲染平台哪个好?价格、速度、稳定性全网真实对比(含渲染101)
ue5·图形渲染·blender·maya·houdini
zhangzhangkeji2 天前
UE5 材质-27-各种节点:round 四舍五入节点,材质依附的物体的纹理的坐标 TextureCoordinate 节点
ue5
成都渲染101云渲染66662 天前
Blender 云渲染平台实测对比:iRender、GarageFarm、渲染101 哪家更适合动画云渲染?
ue5·图形渲染·blender·maya·houdini
Unity打怪升级3 天前
【Unity精品源码】Ultimate Character Controller:高级角色控制器完整解决方案
游戏·unity·ue5·游戏引擎·godot·游戏程序·cocos2d
zhangzhangkeji4 天前
UE5 蓝图-游老师-18-蓝图的封装,19-继承,20-多态:父类中的变量,函数、事件,分发器,实现的接口,可以被子类直接使用,也可以被子类重写重定义
ue5