Online-safe tile movement.

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 ? :grinning:

(I also thought of using MoveToLocation() but this function is not replicated with the SavedMove architecture making it very vulnerable to lag/cheat)

grid movement is predictable, you know the exact start and end so rather than replicate each movement tick you could just replicate the move command and each client can interp smoothly

1 Like

That’s interesting and how would you implement this ?

The movement is not smooth on the owning client, not necessarily on the simulated proxies.

lots of ways but simply you could use the exact code and run in separately on server/client instead of replicating movement.

this way you’re only calling the beginmove event as an RPC. at the end move you could have the server confirm they are synced but there isnt any real reason they should desync

1 Like

I’m having trouble understanding. Sorry, in still figuring this stuff out.

Can you write me an example of how you would do it in C++ ?

try using MoveComponentTo() on the root component, the problem with addmovementinput is you can overshoot the target which is why you had the problem with it ‘missing’

Thank you, i’ll look into it and let you know

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.