UE5学习笔记4-让charactor动起来

一、环境说明,UE5.4 + vs2022 + win11

二、添加操作映射和轴映射

        1.点击编辑->项目设置->输入->找到轴映射和操作映射,添加如图所示的五个地方   

 三、使用C++函数和设置的映射绑定

        xxx.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "BlasterCharacter.generated.h"

UCLASS()
class BLASTER_API ABlasterCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	ABlasterCharacter();	
	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	/* 人物移动函数 */
	void MoveForward(float Value);
	void MoveRight(float Value);
	void Turn(float Value);
	void LookUp(float Value);
	/* */

private:
	/** 弹簧臂组件类声明 属性设置为在任何地方可视 类别是摄像机 */
	UPROPERTY(VisibleAnywhere, Category = Camera)
	class USpringArmComponent* CameraBoom;

	/** 摄像机类声明 属性设置为在任何地方可视 类别是摄像机 */
	UPROPERTY(VisibleAnywhere, Category = Camera)
	class UCameraComponent* FollowCamera;

public:	

};

        xxx.cpp

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


#include "BlasterCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Components/WidgetComponent.h"

// Sets default values
ABlasterCharacter::ABlasterCharacter()
{
 	// 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;

	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));/** 创建类 */
	CameraBoom->SetupAttachment(GetMesh());/** SetupAttachment()将弹簧臂固定在网格上 GetMesh()获得角色的网格(胶囊体) */
	CameraBoom->TargetArmLength = 600.f;/** 设置臂长 */
	CameraBoom->bUsePawnControlRotation = true;/** 是否控制旋转 */

	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	/** 设置附件 将摄像机连接到弹簧臂上,通过USpringArmComponent的指针和USpringArmComponent的名字USpringArmComponent::SocketName*/
	FollowCamera->SetupAttachment(CameraBoom,USpringArmComponent::SocketName);
	/** 跟随摄像头无需使用旋转 旋转在CameraBoom  CameraBoom是FollowCamera的组件 */
	FollowCamera->bUsePawnControlRotation = false;

	bUseControllerRotationYaw = false; /** 不希望角色和控制器一起旋转 */
	GetCharacterMovement()->bOrientRotationToMovement = true; /** 使角色按照原有的方向运动 */
}

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

}

// Called to bind functionality to input
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

    /* 绑定的名字和界面设置中的名字相同 */
	/* 绑定动作映射 */
	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);/* 具有一个输入的事件IE_Pressed */
	/* 绑定轴映射 */
	PlayerInputComponent->BindAxis("MoveForward",this,&ABlasterCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight);
	PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn);
	PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp);
}


// Called when the game starts or when spawned
void ABlasterCharacter::BeginPlay()
{
	Super::BeginPlay();
	
}

void ABlasterCharacter::MoveForward(float Value)
{
	/* Controller理解成一个人物的控制器 */
	if (Controller != nullptr && Value != 0.f)
	{
		/* 获得旋转方向 */
		const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
		/* 从旋转方向创建旋转矩阵FRotationMatrix(YawRotation) 称其为单位轴GetUnitAxis(EAxis::X),返回一个F向量 */
		const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X));
		/*沿给定世界方向向量(通常归一化)添加运动输入,按 'ScaleValue' 缩放。
		如果 ScaleValue < 0,则移动方向相反。Base Pawn 类不会自动应用移动,
		在 Tick 事件中,这取决于用户是否这样做。Character 和 DefaultPawn 等子类会自动处理此输入并移动*/
		AddMovementInput(Direction, Value);
	}
}

void ABlasterCharacter::MoveRight(float Value)
{
	if (Controller != nullptr && Value != 0.f)
	{
		const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
		const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y));
		/*沿给定世界方向向量(通常归一化)添加运动输入,按 'ScaleValue' 缩放。
		如果 ScaleValue < 0,则移动方向相反。Base Pawn 类不会自动应用移动,
		在 Tick 事件中,这取决于用户是否这样做。Character 和 DefaultPawn 等子类会自动处理此输入并移动*/
		AddMovementInput(Direction, Value);
	}
}

void ABlasterCharacter::Turn(float Value)
{
	/*如果是本地 PlayerController,
	则将输入(影响 Yaw)添加到控制器的 ControlRotation。
	此值乘以 PlayerController 的 InputYawScale 值。*/
	AddControllerYawInput(Value);
}

void ABlasterCharacter::LookUp(float Value)
{
	/*如果它是本地 PlayerController,
	则将输入(影响 Pitch)添加到控制器的 ControlRotation。
	此值乘以 PlayerController 的 InputPitchScale 值*/
	AddControllerPitchInput(Value);
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值