当前位置:网站首页>如何在 UE4 中用代码去控制角色移动
如何在 UE4 中用代码去控制角色移动
2022-07-30 16:04:00 【华为云】
前言
上文中我们已经在场景中摆放好了游戏角色,接下来要做的工作就是要让游戏角色在场景中让它跑动起来,今天,我们就来实现这一功能,通过键盘的 W A S D 键来实现角色的自由跑动。
步骤
要让角色跑动的时候,有一个第三人称的视角,我们首先需要给角色添加一个摄像机。
添加摄像机
在组件中添加摄像机。

移动摄像机和角色,是的角色和摄像机的朝向都要面对着蓝色箭头。

然后保存设置。
创建 GameMode
点击 File - New Class,然后创建 GameMode,命名为 HeroGameMode,这样你的 VS 工程中就会多出一个 HeroGameMode 类。

创建 GameMode 的蓝图
回到我们的场景中,选择 Blueprints, 新建一个 HeroGameMode 蓝图。

这里命名为 BP_HeroGameMode。

指定角色蓝图
在右侧的细节面板 Classes - Default Pawn Class 中指定我们之前创建好的角色蓝图 BP_Hero。

然后保存。
设置键盘输入
点击项目设置,点击左侧面板的 input,然后按照如图所示添加按键映射。

代码控制
在 vs 中上文生成的 Hero 类中,添加如下代码:
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"#include "GameFramework/Character.h"#include "Hero.generated.h"UCLASS()class ACTOREXAMPLE_API AHero : public ACharacter{ GENERATED_BODY()public: // Sets default values for this character's properties AHero();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; void MoveForward(float amount); void MoveBack(float amount); void MoveLeft(float amount); void MoveRight(float amount); void Yaw(float amount); void Pitch(float amount);};// Fill out your copyright notice in the Description page of Project Settings.#include "Hero.h"// Sets default valuesAHero::AHero(){ // 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;}// Called when the game starts or when spawnedvoid AHero::BeginPlay(){ Super::BeginPlay(); }// Called every framevoid AHero::Tick(float DeltaTime){ Super::Tick(DeltaTime);}// Called to bind functionality to inputvoid AHero::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){ Super::SetupPlayerInputComponent(PlayerInputComponent); PlayerInputComponent->BindAxis("Forward", this, &AHero::MoveForward); PlayerInputComponent->BindAxis("Back", this, &AHero::MoveBack); PlayerInputComponent->BindAxis("Left", this, &AHero::MoveLeft); PlayerInputComponent->BindAxis("Right", this, &AHero::MoveRight); PlayerInputComponent->BindAxis("Yaw", this, &AHero::Yaw); PlayerInputComponent->BindAxis("Pitch", this, &AHero::Pitch);}void AHero::MoveForward(float amount){ if (Controller && amount) { FVector fwd = GetActorForwardVector(); AddMovementInput(fwd, amount); }}void AHero::MoveBack(float amount){ if (Controller && amount) { FVector back = -GetActorForwardVector(); AddMovementInput(back, amount); }}void AHero::MoveLeft(float amount){ if (Controller && amount) { FVector left = -GetActorRightVector(); AddMovementInput(left, amount); }}void AHero::MoveRight(float amount){ if (Controller && amount) { FVector right = GetActorRightVector(); AddMovementInput(right, amount); }}void AHero::Yaw(float amount){ if (Controller && amount) { AddControllerYawInput(200.0f * amount * GetWorld()->GetDeltaSeconds()); }}void AHero::Pitch(float amount){ if (Controller && amount) { AddControllerPitchInput(200.0f * amount * GetWorld()->GetDeltaSeconds()); }}这样引擎就会检测到我们之前配置的按键输入,按下 W 键,就会去调用类中的 MoveForward 函数,其他的几个按键也是如此。
运行
按下 Play 后,通过控制 W A S D 按键,你就可以控制角色的自由跑动啦!

最后
到这里本次的教程就结束了,接下来咱们就来复盘一下本次所说的内容。
- 添加摄像机来得到一个第三人称的视角
- 创建 GameMode 类和 GameMode 蓝图,并指定好角色蓝图
- 设置键盘输入映射
- 编写代码来映射键盘,控制角色行走
是不是很简单,好了,今天就到这吧!
我是杰少,如果您觉的我写的不错,那请给我 点赞+评论+收藏 后再走哦!
请你喝杯 ️ 点赞 + 关注哦~
- 阅读完记得给我点个赞哦,有 有动力
- 关注公众号— HelloWorld杰少,第一时间推送新姿势
最后,创作不易,如果对大家有所帮助,希望大家点赞支持,有什么问题也可以在评论区里讨论~**
边栏推荐
- Image information extraction DEM
- 新人学习小熊派网络应用开发
- [AGC] Quality Service 1 - Example of Crash Service
- Golang分布式应用之Redis怎么使用
- arcpy tutorial
- MySql 和 PostgreSQL 数据库 根据一张表update另一张表数据
- [HMS core] [FAQ] A collection of typical questions about push kit, analysis services, and video editing services 3
- rhce笔记2
- [HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2
- How to intercept the first few digits of a string in php
猜你喜欢
![[HMS core] [FAQ] A collection of typical questions about push kit, analysis services, and video editing services 3](/img/7a/e7447b21421f3052c0b6c44d1c2817.png)
[HMS core] [FAQ] A collection of typical questions about push kit, analysis services, and video editing services 3
![[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍](/img/29/92b9d52d17a203b8bdead3eb2c902e.png)
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
![[TypeScript] Introduction, Development Environment Construction, Basic Types](/img/d7/b3175ab538906ac1b658a9f361ba44.png)
[TypeScript] Introduction, Development Environment Construction, Basic Types
![[HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2](/img/08/9f2c7d1ea704f234c2a1882f85df24.png)
[HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2

SMI 与 Gateway API 的 GAMMA 倡议意味着什么?

FME's scheme and operation process for reading and writing cass data

AI遮天传 DL-CNN

Golang分布式应用定时任务如何实现

【AGC】质量服务2-性能管理示例

Pytorch 训练技巧
随机推荐
AL遮天传 DL-深度学习模型的训练技巧
服务器装好系统的电脑怎么分区
【HMS core】【FAQ】Account、IAP、Location Kit and HarmonyOS典型问题合集1
【AGC】Open Test Example
【AGC】质量服务2-性能管理示例
【HMS core】【FAQ】push kit、分析服务、视频编辑服务典型问题合集3
3D激光SLAM:LeGO-LOAM论文解读---实验对比
MySql 和 PostgreSQL 数据库 根据一张表update另一张表数据
php如何查询字符串出现位置
华为云WeLink携手伙伴,共建协同办公生态
Redis 复习计划 - Redis 数据结构和持久化机制
rhce笔记3
vivo announced to extend the product warranty period, the system launched a variety of functional services
FME读写cass数据的方案及操作流程
[flutter]什么是MaterialApp和Material design
Leetcode 119. 杨辉三角 II
(1) Cloud computing technology learning - virtualized vSphere learning
TiUP FAQ
04、Activity的基本使用
五只小猪的案例(五只小猪 比较体重的大小)