APawn::GetMovementComponent
#include "GameFramework/Pawn.h"
Access: public
Specifiers: virtualUFUNCTIONBlueprintCallable
Description
Returns the pawn's movement component, if it has one. The default implementation scans all components for the first UPawnMovementComponent; native subclasses should override this for efficiency.
Caveats & Gotchas
- • The base APawn implementation iterates all components each call, which is O(n). If you call this frequently (e.g., every tick), override it in your subclass to return a cached pointer instead.
- • Returns nullptr for Pawns that have no movement component — always null-check the result before calling methods on it.
- • ACharacter caches its CharacterMovementComponent and overrides this function, so the base scan only happens for custom APawn subclasses that forget to override.
Signature
virtual UPawnMovementComponent* GetMovementComponent() const Return Type
UPawnMovementComponent* Examples
Cast movement component to CharacterMovementComponent and set max walk speed
Blueprint
Check velocity cap before adding input C++
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (UPawnMovementComponent* MoveComp = GetMovementComponent())
{
if (MoveComp->Velocity.SizeSquared() < MaxSpeedSq)
{
AddMovementInput(GetActorForwardVector());
}
}
} Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?