How would you add a custom movement component to a Character?

I want to add a gliding capability to my character. I had a look at the character movement component and there is a option for custom movement CustomMovementMode | Unreal Engine Documentation but I have no idea how to integrate gliding with this.

I would a create my own movement component which composes all my custom movement components



class MyCustomMovement{
   CharacterMovementComponent cm;
   GlidingMovementComponent gm;
   SlidingMovementComponent sm;
   ...
};


I am wondering if there is a better way to customize the CharacterMovementComponent?

The CharacterMovementComponent already has a few different movement modes, Flying(simple), Swimming, Falling, Walking and most important for your needs Custom.

You’ll want to create a new class inheriting from CharacterMovementComponent. You call SetMovementMode(EMovementMode::Custom, MyUint8CustomMode) on the character movement component when you want to glide. That frees you of interference from the default modes. Override OnMovementModeChanged, check if it is your custom mode

 
void UMyMovementComponent::OnMovementModeChanged(EMovementMode prevMovement, uint8 prevCustomMovement)
{
     Super::OnMovementModeChanged(prevMovement,PrevCustomMovement);

     //Should probably set up an enum cast to uint8 for checking against custom mode (uint8)EMyCustomMovement::GLIDE
     if(MovementMode==EMovementMode::Custom &&  CustomMovementMode == MyGlideUint8)
     {   
          //Set my glider settings
     }
}

Download the glider project from the marketplace. It doesn’t use a movement mode and is a blueprint project, but it has the basic physics down for gliding. It’s called “Landscape Mountains”

if you want to go the CUstomMovementComponent route you need to do the following:

In your Character Class you need to add this to the constructor (BRS is the prefix I’m using for our project, you would need to insert your own class names instead of mine)


ABRSCharacter::ABRSCharacter(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP.SetDefaultSubobjectClass<UBRSCharacterMovementComp>(ACharacter::CharacterMovementComponentName))
{
    // Other Stuff ...

This will make the Character use your custom MovementComponent Class.

One important function would be PhysCustom in CharacterMovementComponent. Override it and call your custom MovementPhysics from there:


void UBRSCharacterMovementComp::PhysCustom(float deltaTime, int32 Iterations)
{
	switch (CustomMovementMode)
	{
	case CMOVE_Slide:
		PhysCustomSlide(deltaTime, Iterations);
		break;
	case CMOVE_Roll:
		PhysCustomRoll(deltaTime, Iterations);
		break;
	case CMOVE_WallRun:
		PhysCustomWallrun(deltaTime, Iterations);
		break;
	case CMOVE_WallClimb:
		PhysCustomWallclimb(deltaTime, Iterations);
		break;
	case CMOVE_LedgeClimb:
		PhysLedgeClimb(deltaTime, Iterations);
		break;
	case CMOVE_None:
	default:
		break;
	}
}

This should be the important stuff.

CMOVE_… where defined using #define to make the code more readable

1 Like

How should I construct the Custom CharacterMovementComponent so that I can call its functions? To use the above example, how do I make sure PhysCustomSlide is being called if I call


GetCharacterMovement()->SetMovementMode(MOVE_Custom, CMOVE_Slide)

Wouldn’t that call the non-overridden member PhysCustom()?