UE5 C++(十三)— 创建Character,添加增强输入

创建Character第三人称模板

创建MyCharacter C++类
在这里插入图片描述
在这里插入图片描述

添加增强输入引用

在DEMO.Build.cs 脚本中添加增强输入模块
在这里插入图片描述
有个容易出错的点,这里的设置一定要正确
在这里插入图片描述
然后添加引用到C++头文件中

#include "InputActionValue.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"

最后,可以编译一下,看看输入模块(“EnhancedInput” )是否引入成功。

在脚本中实现移动、旋转

首先,是头文件添加引用
MyCharacter.h

#pragma once

#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"

UCLASS()
class DEMO_API AMyCharacter : public ACharacter
{
   
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMyCharacter();

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;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
		USpringArmComponent *MySpringArm;
	UPROPERTY(EditAnywhere, 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;

	void Move(const FInputActionValue& Value);
	void Look(const FInputActionValue& Value);
};

然后在MyCharacter.cpp中实现

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyCharacter.h"

// Sets default values
AMyCharacter::AMyCharacter()
{
   
	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));
	MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
	MySpringArm->SetupAttachment(RootComponent);
	MyCamera->SetupAttachment(MySpringArm);
	MySpringArm->TargetArmLength = 300.0f;
	// 这么设置是为了让控制器的转动不影响角色的转动,只影响摄像机的转动
	bUseControllerRotationYaw = false;
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;
	// 为了让角色的移动方向与摄像机的方向一致,需要设置以下参数
	GetCharacterMovement()->bOrientRotationToMovement = true;
	// 这是为了使用Pawn的控制器旋转
	MySpringArm->bUsePawnControlRotation = true;
}

// Called when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
   
	Super::BeginPlay();
	APlayerController *PlayerController = Cast<APlayerController>(GetController());
	if (PlayerController)
	{
   
		UEnhancedInputLocalPlayerSubsystem *LocalPlayerSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());
		if (LocalPlayerSubsystem)
		{
   
			LocalPlayerSubsystem->AddMappingContext(DefaultMappingContext, 0);
		}
	}
}

// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
   
	Super::Tick(DeltaTime);
}

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

void AMyCharacter::Move(const FInputActionValue &Value)
{
   
	FVector2D MoveVector = 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, MoveVector.Y);
		AddMovementInput(RightDirection, MoveVector.X);
	}
}

void AMyCharacter::Look(const FInputActionValue &Value)
{
   
	FVector2D LookVector = Value.Get<FVector2D>();
	if (Controller == nullptr)
	{
   
		return;
	}
	AddControllerYawInput(LookVector.X);
	AddControllerPitchInput(LookVector.Y);
}

编译之后,创建蓝图类BP_MyCharacter
在这里插入图片描述
这时候会有默认组件,缺少人物模型,我们可以在这里添加
在这里插入图片描述
添加动画蓝图类
在这里插入图片描述
其他设置
在这里插入图片描述
在这里插入图片描述

最后,拖到场景中,把WorldSetting -> Game Mode 设置为null
在这里插入图片描述
点击运行,就可以实现鼠标,键盘第三人称操作了。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-09 10:42:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-09 10:42:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-09 10:42:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-09 10:42:03       18 阅读

热门阅读

  1. Django创建RSS订阅

    2024-01-09 10:42:03       35 阅读
  2. 网络协议到底是什么?

    2024-01-09 10:42:03       35 阅读
  3. js中window的OPen方法,弹窗的特征

    2024-01-09 10:42:03       34 阅读
  4. 每日算法打卡:激光炸弹 day 8

    2024-01-09 10:42:03       32 阅读
  5. 【python】神经网络

    2024-01-09 10:42:03       36 阅读
  6. matlab subs 函数计算太慢

    2024-01-09 10:42:03       42 阅读
  7. Serverless架构的理论基础和发展历程

    2024-01-09 10:42:03       35 阅读
  8. 【flink番外篇】14、Flink异步I/O访问外部数据示例

    2024-01-09 10:42:03       40 阅读
  9. Docker的基本概念和优势

    2024-01-09 10:42:03       34 阅读
  10. OpenCV 配置选项参考(一)

    2024-01-09 10:42:03       46 阅读