UE4 set character movement and character Perspective

Posted by Tiger99 on Thu, 27 Jan 2022 05:18:53 +0100

UE4 set character movement and character Perspective

In this section, we first explain the settings of UE4 character movement, and then explain the settings of UE4 character perspective.

UE4 set character movement

  1. Open UE4 editor, click "Edit" and then click "project settings":

  1. Select input:

  1. Click the add key input of "axismaps" to create 6 key inputs:

  1. Change the name of keys and keys. Here, one key corresponds to one name. You can bind different events by adding multiple methods with different names but the same keys.

  1. After setting, we open the VS editor and declare four functions under the "PlayingCharacter.h" file:

These four functions are the functions we want to realize the mobile function. Here we come cpp file to implement these four functions

void APlayingCharacter::MoveForward(float val)
{
    AddMovementInput(GetActorForwardVector(), val);
}
void APlayingCharacter::MoveBack(float val)
{
    AddMovementInput(-GetActorForwardVector(), val);
}
void APlayingCharacter::MoveRight(float val)
{
    AddMovementInput(GetActorRightVector(), val);
}
void APlayingCharacter::MoveLeft(float val)
{
    AddMovementInput(-GetActorRightVector(), val);
}

AddMovementInput() function will move the character according to the value of the first parameter. The second parameter is a floating point number. If the number is 1, it will be added in the direction of the first parameter. If the second parameter is - 1, it will be added in the opposite direction of the first parameter.

So how does the value of this val variable come from? It has been set when we set the key input. The default value is 1.

GetActorForwardVector() is the forward vector obtained from character A in world space. It obtains the vector of X axis.

GetActorRightVector() is the same as above. It obtains the vector of Y axis.

After writing these four functions, we also need to bind the keys to bind our functions with our keys.

In the "SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)" function, we add four key bindings.

InputComponent is a component used to bind key input.

The first parameter should be the same as the name of the key binding we set at the beginning. The second parameter is a pointer type. We give it directly to this. The third parameter is the function we want to bind.

UE4 set personas

We also write our code to control the perspective in the setupplayerinputcomponentet function.

AddControllerYawInput() and AddControllerPitchInput() are the functions of the mobile perspective that UE4 has encapsulated for us.

FRotator

Yaw said shaking his head means moving around Z;

Pich means nodding is moving around Y;

Roll, you can imagine shaking your head left and right, moving around the X-axis.

These three variables constitute the direction of rotation of all objects in UE4. It is a structure called FRotator.

After writing the above code, click compile, return to UE4 and click Play. We will find that the characters can move back and forth and look up and down.

Full code:

APlayingCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "PlayingCharacter.generated.h"
UCLASS()
class GAMEPROJECT_API APlayingCharacter : public ACharacter
{
    GENERATED_BODY()
public:
    // Sets default values for this character's properties
    APlayingCharacter();
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 val);//The character moves forward
    void MoveBack(float val);    //Character backward
    void MoveRight(float val);    //Character right
    void MoveLeft(float val);    //Character left
};
APlayingCharacter.Cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayingCharacter.h"
// Sets default values
APlayingCharacter::APlayingCharacter()
{
     // 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 APlayingCharacter::BeginPlay()
{
    Super::BeginPlay();
   
}
// Called every frame
void APlayingCharacter::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APlayingCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
    InputComponent->BindAxis("MoveForward", this, &APlayingCharacter::MoveForward);
    InputComponent->BindAxis("MoveBack", this, &APlayingCharacter::MoveBack);
    InputComponent->BindAxis("MoveRight", this, &APlayingCharacter::MoveRight);
    InputComponent->BindAxis("MoveLeft", this, &APlayingCharacter::MoveLeft);
    InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
    InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}
void APlayingCharacter::MoveForward(float val)
{
    AddMovementInput(GetActorForwardVector(), val);
}
void APlayingCharacter::MoveBack(float val)
{
    AddMovementInput(-GetActorForwardVector(), val);
}
void APlayingCharacter::MoveRight(float val)
{
    AddMovementInput(GetActorRightVector(), val);
}
void APlayingCharacter::MoveLeft(float val)
{
    AddMovementInput(-GetActorRightVector(), val);
}

Topics: IDE Visual Studio UE4