Replicating Rotation in C++

Hi there,

I know versions of this question have been asked many times before, but there is no sufficient answer for a C++ programmer, and my code still does not perform as expected.

In C++, I have this first image:

329849-topdowntemplatecode.png

The Character is set to replicate as well. This didn’t work. I had to add this to the client tick:

329850-tick.png

This also doesn’t work. Paradoxically, on the client I issue move commands to, the movement jitters and doesn’t rotate, but every other client sees smooth movement and rotation.

I’ve checked all these boxes in my blueprint subclasses as well, just in case. What am I misunderstanding about replicated variables and UseControllerDesiredRotation? All I’m trying to do is have the controller rotate toward the direction of the PathFollowingComponent.

It’d be a nice plus if someone could help me understand why local movement is jittery but replicated movement is interpolating fine.

Video of behavior here: Client Movement Issues - Album on Imgur

Hey hey, I got this to work, including fully replicated movement using the mouse for top down template.

It was kind of involved, actually. Here are the steps:

  • Take a look at how AIController accesses the navigation system and either A.) Copy this code into your own controller and fix dependencies or B.) Write a similar API for your own PlayerController.

  • Second, and this is optional but easier, send the navigation to the client and allow clients to do their own navigation, this is simpler for replication purposes.

  • Finally, in your PlayerController (or wherever you are controlling your own movement), update rotation similar to this:

    void AMyPlayerController::UpdateRotation(float DeltaTime)
    {
    FRotator CurrentRotation = GetControlRotation();

         if (IsFollowingAPath())
         {
             FVector asDirection = PathFollowingComponent->GetCurrentDirection();
             FRotator asOrientation = asDirection.ToOrientationRotator();
             CurrentRotation= asOrientation;
         }
     
         SetControlRotation(CurrentRotation);
     
         APawn* const P = GetPawnOrSpectator();
         if (P)
         {
             P->FaceRotation(CurrentRotation, DeltaTime);
         }
     }
    

Now here’s the deal, you could try to use Use Controller Desired Rotation in your Character, but I found this to not work reliably with path-based movement. This gave me the correct rotation every time. Also make sure you replicate CharacterMovement. I hope this helps.