Character Movement Component

We're going to make use of UE's CharacterMovementComponent as that already dictates how the character should move, handling acceleration, physics, velocity, replication and basic collision.

Lets extend it to make our own

Custom Character Movement Component

.h

// ZCCharacterMovementComponent

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "ZCCharacterMovementComponent.generated.h"

UCLASS()
class CLIMBING_API UZCCharacterMovementComponent : public UCharacterMovementComponent
{
	GENERATED_BODY()
};

We also need to update our projects Character class by adding an FObjectInitializer param to the default ctor and overriding the default CharacterMovementComponent to instead use our custom class.

.h

// ZCClimbingCharacter.h

AZCClimbingCharacter(const FObjectInitializer& ObjectInitializer);

.cpp

// ZCClimbingCharacter.cpp

AZCClimbingCharacter::AZCClimbingCharacter(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.SetDefaultSubobjectClass<UZCCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
	// ...	
}

Lastly, we need to provide a new accessor to retrieve our custom CharacterMovementComponent as the default ACharacter::GetCharacterMovement() is non virtual (we cannot override it) and returns a UCharacterMovementComponent type

.h

// ZCClimbingCharacter.h

public:
	UFUNCTION(BlueprintCallable)
	FORCEINLINE class UZCCharacterMovementComponent* GetZCMovementComponent() const { return MovementComponent; }

protected:
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Character")
	class UZCCharacterMovementComponent* MovementComponent

.cpp

// ZCClimbingCharacter.cpp

AZCClimbingCharacter::AZCClimbingCharacter(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.SetDefaultSubobjectClass<UZCCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
	// ...
	MovementComponent = Cast<UZCCharacterMovementComponent>(GetCharacterMovement());
}

Understanding the Character Movement Component

UE's documentation on the class here Or (requires EpicGames' approval) access to UE source here

// CharacterMovementComponent.h

CharacterMovementComponent handles movement logic for the associated Character owner. 
It supports various movement modes including: walking, falling, swimming, flying, 
custom.

Summarizing the primary actions of CharacterMovementComponent

Tick

In a non-networked game PerformMovement() executes every tick

PerformMovement

PerformMovement() is the top level function which

  • applies external physics like impulses, forces, and gravity

  • calculates movement from animation root motion

  • calls StartNewPhyics

StartNewPhys

StartNewPhyics() selects the correct Phys* function based off the movement mode

i.e. if the Movement Mode is Walking, then PhysWalking() will execute

Phys*

Each Movement Mode has a corresponding Phys*() function including PhysCustom (which we will use later on)

If the movement mode changes during a tick, such as if a character starts falling or colliding with an object, Phys* will re-call StartNewPhysics so the updated Phys* function logic will execute

OnMovementUpdated

an Event triggered at the end of a movement update and is the preferred event to use when performing custom updates

SetMovementMode

Additionally it's quite easy for external classes to change the type of movement by using SetMovementMode() which fires the OnMovementModeChanged() event notifying the character component to change movement

PhysCustom

Like mentioned the PhysCustom is intended to be overridden to enable developers to hook in their own custom phyics movement types and logic

  • We will be utilizing this for our Climbing logic

Last updated