If CharacterMovement shouldn't be accessed directly, how can I modify Velocity?

For ages, my standard method of moving a character has been some variant of this:

 ACharacter* myCharacter = Cast<ACharacter>(MeshComp->GetOwner());
 FVector movementVector = myActor->GetActorForwardVector()*moveDistance;
 movementVector = movementVector.RotateAngleAxis(moveDirection, FVector(0, 0, 1));
 myCharacter->CharacterMovement->Velocity += movementVector;

However, recently the last line, where I directly add to the character’s velocity, produces a compiler warning that character movement should not be accessed directly. I looked through UCharacterMovementComponent | Unreal Engine Documentation , but I don’t see a setter for velocity- if it’s going to be set to private soon, how are we intended to set the velocity from code?

GetMovementComponent should return a UPawnMovementComponent which you can cast to UCharacterMovementComponent.

Oooh that does it, thank you- I was looking directly in charactermovement, not for a different component type :slight_smile:

No problem!

/** Returns CharacterMovement subobject */
class UCharacterMovementComponent
ACharacter::GetCharacterMovement() const;

Besides you should use

APawn::AddMovementInput

to control character movement and tune UCharacterMovementComponent parameters( MaxWalkSpeed etc).