Hello. I moved all the character control logic into a separate component. In this component I have two methods.
AController* GetController() const { return Cast<APawn>(GetOwner())->GetController(); };
APawn* GetPawn() const { return Cast<APawn>(GetOwner()); }
They are called when I move.
void UFPInputHandlerComponent::Move(const FInputActionValue& Value)
{
const FVector2D MovementVector = Value.Get<FVector2D>();
const FRotator Rotation = GetController()->GetControlRotation();
const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
GetPawn()->AddMovementInput(ForwardDirection, MovementVector.Y);
GetPawn()->AddMovementInput(RightDirection, MovementVector.X);
}
I have two questions:
- How expedient is it to move the movement logic into a separate component?
- How expensive is it to cast literally every frame when I move?