上文中我们已经在场景中摆放好了游戏角色,接下来要做的工作就是要让游戏角色在场景中让它跑动起来,今天,我们就来实现这一功能,通过键盘的 W A S D 键来实现角色的自由跑动。
要让角色跑动的时候,有一个第三人称的视角,我们首先需要给角色添加一个摄像机。
在组件中添加摄像机。
image
移动摄像机和角色,是的角色和摄像机的朝向都要面对着蓝色箭头。
image
然后保存设置。
点击 File - New Class,然后创建 GameMode,命名为 HeroGameMode,这样你的 VS 工程中就会多出一个 HeroGameMode 类。
image
回到我们的场景中,选择 Blueprints, 新建一个 HeroGameMode 蓝图。
image
这里命名为 BP_HeroGameMode。
image
在右侧的细节面板 Classes - Default Pawn Class 中指定我们之前创建好的角色蓝图 BP_Hero。
image
然后保存。
点击项目设置,点击左侧面板的 input,然后按照如图所示添加按键映射。
image
在 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 values
AHero::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 spawned
void AHero::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AHero::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void 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 按键,你就可以控制角色的自由跑动啦!
image
到这里本次的教程就结束了,接下来咱们就来复盘一下本次所说的内容。
是不是很简单,好了,今天就到这吧!
本文分享自 HelloWorld杰少 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有