Hello, I would be really glad if you could point me in the right direction here - I am making a multiplayer top down game where you control the rotation of mage via the placement of mouse on screen. The problem is I am having great difficulty in syncing/replicating the rotations across client and server. So this code means that the server player can rotate and the rotations are replicated across to the client. The client can rotate but it isn’t then replicated across back to the server. I have a feeling I need to replicate something else as well but I don’t know what that would be. Either that because this is movement I need to have it in a different class? So first I take a HitResultUnderCursor in the MagePlayerController class and push it down into the Mage character class.
The PlayerController Code (I am pretty sure this is right and not the problem)
void AMagePlayerController::AimTowardsMouse()
{
// So that the hitresult code only runs on local player controllers
if(Role<ROLE_Authority)
{
FHitResult hitResult; //OUT parameters
GetHitResultUnderCursor(ECC_Visibility, true, hitResult);
//if it can deproject the mouse onto the world then
FVector HitLocation = hitResult.Location;
GetControlledMage()->AimAtMouse(HitLocation); //passes the location down
}
}
This is my current code for rotation. I think this is where the problem lies. Not with the rotation itself but how its called on the players most likely as I feel I don’t have a solid grip on networking yet.
This is the code is Mage Character class (SetReplicates(true) and SetReplicateMovement(true))
void AMage::AimAtMouse(FVector MouseLocation)
{
if (Role < ROLE_Authority)
{
ServerAimToMouse(MouseLocation);
}
if (!Staff) { return; }
if (!Mage) { return; }
FVector MageLocation = GetActorLocation();
FVector AimDirection = MouseLocation - MageLocation;
//Gets the vector between the two points
AimDirection = AimDirection.GetSafeNormal();
// gets the normalised vector of the mouse
// Rotate via yaw
FVector MageForwardVector = GetActorForwardVector();
FRotator MageRotator = MageForwardVector.Rotation();
FRotator rotate(0, 90, 0);
// Adding 90 degrees means at 270 degrees it messes up as it goes over 0
MageRotator = MageRotator + rotate;
// makes it so the head points to the mouse
FRotator AimAsRotator = AimDirection.Rotation();
//turns the unit vector into a Rotation, Roll set to 0
FRotator DeltaRotator = AimAsRotator - MageRotator; //gets the difference
auto RelativeSpeed = DeltaRotator.Yaw;
AddActorWorldRotation(FRotator(0, RelativeSpeed, 0));
}
The server Implementation is
void AMage::ServerAimToMouse_Implementation(FVector MouseLocation)
{
AimAtMouse(MouseLocation);
}
Thank you for reading, I hope I wrote the question well, any help with amending the question with more useful info/better wording and help with the question itself is appreciated.