So I launch projectiles in direction of my camera. However I noticed that client’s spring arm/camera only moves in its own viewport and is stuck in starting position from server’s viewport. How can I fix this(make server update client’s spring arm rotation)?
Server’s viewport(Camera should be lower as you can see in client’s viewport below):
Client’s Viewport:
Here’s all the code I use for replicating spring arm rotation:
Spring arm constructor
// Create a camera boom...
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(RootComponent);
SpringArm->SetUsingAbsoluteRotation(true);
SpringArm->TargetArmLength = 400.f;
SpringArm->SetRelativeRotation(SpringArmRotation);
SpringArm->bDoCollisionTest = true;
SpringArm->bEnableCameraLag = true;
SpringArm->CameraLagSpeed = 8;
SpringArm->CameraLagMaxDistance = 1.f;
SpringArm->bEnableCameraRotationLag = true;
SpringArm->CameraRotationLagSpeed = 8;
SpringArm->CameraLagMaxTimeStep = 1;
ReplicatedProps:
void APlayerCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(APlayerCharacter, SpringArmRotation);
}
OnRep Function and spring arm update functions:
void APlayerCharacter::OnRep_SpringArmRotation()
{
OnSpringArmRotationUpdate();
}
void APlayerCharacter::OnSpringArmRotationUpdate()
{
SpringArm->SetWorldRotation(SpringArmRotation);
}
Set function for horizontal axis:
void APlayerCharacter::SetSpringArmRotation(float rotationValue)
{
if (IsLocallyControlled())
{
SpringArmRotation = SpringArmRotation + FRotator(0, rotationValue, 0);
OnSpringArmRotationUpdate();
FString healthMessage = FString::Printf(TEXT("%s"), *SpringArmRotation.ToString());
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, healthMessage);
}
if (GetLocalRole() == ROLE_Authority)
{
SpringArmRotation = SpringArmRotation + FRotator(0, rotationValue, 0);
OnSpringArmRotationUpdate();
FString healthMessage = FString::Printf(TEXT("%s"), *SpringArmRotation.ToString());
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, healthMessage);
}
}
Set function for vertical axis
void APlayerCharacter::SetSpringArmVerticalRotation(float rotationValue)
{
if (IsLocallyControlled())
{
if(SpringArmRotation.Pitch + rotationValue<40 && SpringArmRotation.Pitch + rotationValue>-90)
SpringArmRotation = SpringArmRotation + FRotator(rotationValue, 0, 0);
OnSpringArmRotationUpdate();
}
if (GetLocalRole() == ROLE_Authority)
{
if(SpringArmRotation.Pitch + rotationValue<40 && SpringArmRotation.Pitch + rotationValue>-90)
SpringArmRotation = SpringArmRotation + FRotator(rotationValue, 0, 0);
OnSpringArmRotationUpdate();
}
}
Input functions for horizontal and vertical rotations
void APlayerCharacter::RotateCameraVertical(float AxisValue)
{
SetSpringArmVerticalRotation(AxisValue);
}
void APlayerCharacter::RotateCameraHorizontal(float AxisValue)
{
SetSpringArmRotation(AxisValue);
}