Hi!
I want to create a grid-based movement where my character smoothly goes from one tile to another.
Since this is the basis of an online multiplayer game, I need it to be sharp so I’m using the Character Movement Component and AddMovementInput() to make use of the SavedMove architecture.
Here what I have so far
void AFighter::MoveToNextTile(FVector TargetPosition, float MovementSpeed) {
GetWorldTimerManager().ClearTimer(MovementTimerHandle);
// Calculate the direction to move
FVector MoveDirection = (TargetPosition - GetActorLocation()).GetSafeNormal();
// Start a timer to handle movement
GetWorldTimerManager().SetTimer(MovementTimerHandle, [this, TargetPosition, MoveDirection, MovementSpeed]()
{
// Use AddMovementInput for replicated movement
AddMovementInput(MoveDirection, MovementSpeed, false);
if (FVector::Distance(GetActorLocation(), TargetPosition) < 5)
{
FighterMovementComponent->StopMovementImmediately();
SetActorLocation(TargetPosition);
GetWorldTimerManager().ClearTimer(MovementTimerHandle);
}
}, GetWorld()->GetDeltaSeconds(), true);
}
The issue is : The movement is extremely jittery and laggy in online compared to local.
Also, sometimes, the character miss the targetPosition, goes over it and keeps on moving forever. (framerate issues ?)
Does anyone have any idea on how to fix this or if this is the right approach to do grid movement ?
(I also thought of using MoveToLocation() but this function is not replicated with the SavedMove architecture making it very vulnerable to lag/cheat)