I am participating in the nuggets Community game creative submission Contest. For details, please see: Game Creative Submission Contest
preface
Above we have placed the game character in the scene, the next thing to do is to let the game character in the scene to let it run, today, we will realize this function, through the keyboard W A S D keys to realize the role free running.
steps
To have a third-person view of the character as he runs, we first need to add a camera to the character.
Adding a Camera
Add a camera to the component.
Move the camera and the character, yes the character and the camera should be facing the blue arrow.
Then save the Settings.
Create GameMode
Click File-New Class and create GameMode and name it HeroGameMode. This will give you an extra HeroGameMode Class in your VS project.
Create the blueprint for GameMode
Go back to our scene, select Blueprints and create a New HeroGameMode blueprint.
This is called BP_HeroGameMode.
Specify role blueprints
Specify the role blueprint we created earlier, BP_Hero, in the details panel classes-Default Pawn Class on the right.
And then save.
Setting keyboard input
Click Project Settings, click Input in the left pane, and add the key mapping as shown.
Code control
Add the following code to the Hero class generated above in VS:
// 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(a)class ACTOREXAMPLE_API AHero : public ACharacter
{
GENERATED_BODY(a)public:
// Sets default values for this character's properties
AHero(a);protected:
// Called when the game starts or when spawned
virtual void BeginPlay(a) 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);
};
Copy the code
// 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(a)
{
Super::BeginPlay(a); }// 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(a);AddMovementInput(fwd, amount); }}void AHero::MoveBack(float amount)
{
if (Controller && amount)
{
FVector back = -GetActorForwardVector(a);AddMovementInput(back, amount); }}void AHero::MoveLeft(float amount)
{
if (Controller && amount)
{
FVector left = -GetActorRightVector(a);AddMovementInput(left, amount); }}void AHero::MoveRight(float amount)
{
if (Controller && amount)
{
FVector right = GetActorRightVector(a);AddMovementInput(right, amount); }}void AHero::Yaw(float amount)
{
if (Controller && amount)
{
AddControllerYawInput(200.0 f * amount * GetWorld() - >GetDeltaSeconds()); }}void AHero::Pitch(float amount)
{
if (Controller && amount)
{
AddControllerPitchInput(200.0 f * amount * GetWorld() - >GetDeltaSeconds()); }}Copy the code
The engine will detect the key input we configured earlier, press the W key, and call the MoveForward function in the class, as will the other keys.
run
After pressing Play, you can control your character’s free movement by controlling the W A S D button.
The last
That’s the end of the tutorial, let’s go over what we’ve said.
- Add a camera to get a third-person view
- Create the GameMode class and GameMode blueprint, and specify the role blueprint
- Set the keyboard input mapping
- Write code to map the keyboard and control character walking
Isn’t it easy? Well, let’s call it a day!
I am Jie Shao, if you think my writing is good, then please give me a thumbs-up + comments + favorites before leaving oh!
Previous articles:
- How to add game characters to UE4 scenes
- UE4: Android Platform development practice Guide
- UE4 Development Pit Avoidance Guide (continuously updated)
- It’s time to start the New Year. Let’s celebrate with a little fireworks
- Love and Hate with Apple auditors
- Love and Hate with Apple auditors (Part 1)
- A common tool of 2021 | 2021 year-end summary
- Binary tree brush summary: binary search tree properties
- Binary tree summary: binary tree properties
- Binary tree summary: binary tree modification and construction
- StoreKit2 smells this good? Yeah, I tried it. It smells good
- After reading this article, I am no longer afraid of being asked how to construct a binary tree.
- The game guys are trying to get people to pay again. That’s bad!
- Take you rolled a netease holding cloud music home | adapter
- NetEase Cloud Music Home Page (3)
- NetEase Cloud Music Home Page (2)
- NetEase Cloud Music Home Page (a)
- Does the code need comments? Write and you lose
- I would not study in Codable for a long time. They are for fun
- IOS handles web data gracefully. Do you really? Why don’t you read this one
- UICollectionView custom layout! This one is enough
Please drink a cup ☕️ + attention oh ~
- After reading, remember to give me a thumbs-up oh, there is 👍 power
- Follow the public number – HelloWorld Jie Shao, the first time push new posture
Finally, creation is not easy, if it is helpful to you, I hope you can praise and support, what questions can also be discussed in the comments section 😄 ~ **